Comments (4)
You can bind from/to properties on any O.Object instance (most other objects, including O.View, inherit from O.Object so these are all valid targets). You can also bind to (but not from) properties on DOM nodes. For example, el('p', { text: O.bind(person, 'name') });
is creating a <p>
dom node and then binding the text content to the name property on person
. The trouble here is that person is not an instance of O.Object; it's just a plain object. So if you replace { id: 1, name: 'John'}
with new O.Object({ id: 1, name: 'John'})
, then it will work.
However, I don't think that's quite everything you were intending, as it still won't update if you change the contents of the people array; it will just update the text of the appropriate <p>
if the name property changes on one of the items in the array.
An ObservableArray means you can observe when you add/remove elements to it. (It says nothing about whether each item in it is observable; they may be, but don't have to be.) The O.ListView class automatically hooks into this to very efficiently render a list of such objects and keep this updated as the list changes (you could also replace the O.ObservableArray with an O.LiveQuery or O.RemoteQuery if you want to start hooking into the powerful datastore; they all have the same interface).
You need to give O.ListView a O.View class (a class that extends O.View) to render each object. Since all we're doing is setting the text property, this is pretty common and there's already a class that handles this called O.LabelView. The ListView will automatically set the "content" property on each ItemView instance it creates, and O.LabelView automatically keeps the text value of its DOM node in sync with its value property. So if we just extend O.LabelView with the layer tag you wanted (<p>
) and bind the value
property to content.name
, then I think it should do what you're looking for:
var MainView = new O.View({
people : new O.ObservableArray([
new O.Obejct({ id: 1, name: 'John'}),
new O.Object({ id: 2, name: 'Claude'})
]),
draw: function ( layer, Element, el ) {
return el( 'div', [
new O.ListView({
content: this.get( 'people' ),
ItemView: O.Class({
Extends: O.LabelView,
layerTag: 'p',
value: O.bind( 'content.name' )
})
})
])
}
})
new O.RootView(document).insertView(MainView)
from overture.
Thanks for the incredibly detailed explanation, how binding works is now very clear to me. Just one question that's tangentially related, do we have to always use a O.Object
instance's set
method to update an object's property or can we just use normal assignment. I'm asking this because, it seems like a bound html element sometimes updates correctly with normal assignment and sometimes it does not(but using set
seems to always work).
from overture.
You should always use the get/set methods when dealing with O.Object (and anything inheriting from O.Object). Bindings, computed properties and observes will not work if you just do normal assignment.
from overture.
Okay, thanks again.
from overture.
Related Issues (20)
- Documentation rendering HOT 2
- Example Todo: Can not run in Iceweasel HOT 8
- How to/Who will add more views? HOT 1
- RPCSource#handle a public method, function map clarification HOT 2
- Todo Fails to Work HOT 1
- Todo example doesn't run locally
- overture + angular/ionic HOT 5
- search in todo example not working with arabic script HOT 2
- Nested SwitchViews can place their contents out of order
- CSS code for views HOT 1
- performance claims
- Setting properties does not immediately call observers HOT 6
- To do demo app not responding to touch drag and drop HOT 1
- Bind JS Object to a view HOT 1
- Website should be served over HTTPS HOT 1
- Unit tests HOT 1
- Browser Support HOT 2
- Split Loader to a new Repo HOT 1
- Array methods motivation question HOT 1
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 overture.