Comments (8)
from binding.scala.
Could you provide a reproduction code, e.g. a link to https://scalafiddle.io/ ?
from binding.scala.
Working code under 10.0.3: https://scalafiddle.io/sf/bzRRp4g/0
Error under 11.8.1: https://scalafiddle.io/sf/HcOpljv/0
On a side note, I can see the approach that I took isn't very nice so I wanted to ask if there was a better to do what I intend (keep a cache that can both be mutated externally and whenever a cache miss occurs)
from binding.scala.
According to the Scaladoc for the value
method: https://javadoc.io/page/com.thoughtworks.binding/binding_2.11/latest/com/thoughtworks/binding/Binding$$Vars.html#value:scala.collection.mutable.Buffer[A]
This method must not be invoked inside a @dom method body or a Binding { ... } block..
from binding.scala.
It does not make sense to change a Var
or Vars
in a data binding expression that depends on itself. It's like a barber paradox.
As a workaround, you can delay the change by wrapping the value
calls into a Future
:
import com.thoughtworks.binding.Binding._
import com.thoughtworks.binding._
def expensiveOperation(i: Int): Object = "Dummy"
// Cache the operations performed, a Var was needed
// because it could be mutated elsewhere
val objectCache: Vars[(Int,Object)] = Vars.empty
// Index mutated through UI
val selectedIdx: Var[Int] = Var(0)
// Value which is then used to render DOM
val objectValue: Binding[Object] = Binding {
val idx = selectedIdx.bind
val cache = objectCache.bind
cache.find { case (i,_) => i == idx} match {
case Some((_,obj)) => obj
case None => {
val newObject = expensiveOperation(idx)
import concurrent.ExecutionContext.Implicits._
concurrent.Future {
objectCache.value += ((idx, newObject))
}
newObject
}
}
}
Binding {
println(objectValue.bind)
}.watch()
https://scalafiddle.io/sf/HcOpljv/1
Even if you delay the change, recursive data binding expression is still likely buggy, because it may cause endless recalculation.
from binding.scala.
I know AngularJS automatically delay changes, allowing recursive data binding. This approach causes AngularJS's infamous stability problem.
In Binding.scala, recursive data binding requires explicit delay execution with a Future
or setTiemout
. You take the risk when you mean it.
from binding.scala.
Right, I understand.
One thing I'm still unsure about is the typical pattern for input elements which goes like:
import org.scalajs.dom.html
@dom
def inputElement(data: Var[String]): Binding[html.Input] = {
<input oninput={e:Event => data.value = e.target.asInstanceOf[html.Input].value}
value={data.bind} />
}
This is still a recursive binding that works however it still invalidates the principle that value_=
should never be called within a @dom
expression.
Moving the oninput
to an external function kind of resolves it but it still effectively does the same thing (recursive) so I'm still unsure as to how to properly handle this situation (if there is an alternative).
from binding.scala.
from binding.scala.
Related Issues (20)
- FXML: Enable opening pop-up window at a specified location
- Entity references in attribute values do not compile
- Referring to non-existent method com.thoughtworks.binding.FutureBinding$.apply(scala.concurrent.Future,scala.concurrent.ExecutionContext) HOT 1
- Event handlers in custom tags HOT 1
- data prefix does not (it should) set the attribute to null if the initial value is null HOT 1
- FutureBinding compare with React hooks HOT 1
- Make Binding.value be public HOT 16
- Vars.all is not triggerred HOT 2
- := deprecated, how to use value_= ? HOT 3
- dom module is missing for version `12.0.0-M0` HOT 1
- V12 eta HOT 2
- Vars size binding HOT 3
- Conversion from Binding[Iterable[A]] to BindingSeq[A] HOT 4
- Using BindingSeq in an implicit value class HOT 1
- Use @html to create a table, what is returned is scala.xml.NodeBuffer, and what is needed is BindableSeq[org.scalajs.dom.Node], how to convert? HOT 4
- Dependency `scalajs-dom` is out dated HOT 1
- The last element in HTML interpolation are inserted twice (Binding.scala 13.x)
- Code in `ReadMe.md` failed for unkonow reason. HOT 8
- Support for `Scala.js1.x` & `Scala2.12`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from binding.scala.