ember.js - Ember binding works in template but not through extend -


i can bindings work in template like this:

{{view app.myview foobinding:author}} 

but when same extend like this:

app.myview = ember.view.extend     foobinding: "app.applicationcontroller.author" 

it doesn't work.

what missing?

if want bind in view class, should use controller.

as per naming conventions view named authorview authorcontroller or auto-generate basic controller. in code, setting binding class (which represents type) instead of instance (which concrete object).

if create controller, can define author property there, , in view class, can define binding such foobinding: "controller.author" since knows controller instance of authorcontroller.

app.authorcontroller = ember.controller.extend     author: author.create         firstname: 'bill'         lastname: 'buckley'  app.authorview = ember.view.extend     templatename: 'author'     foobinding: "controller.author"     fullname: (->             author = @get 'foo'             "#{author.get('firstname')} #{author.get('lastname')}"     ).property 'firstname', 'lastname' 

this approach force use view.binding.property in handlebars templates:

<script type="text/x-handlebars" data-template-name='author'>     written {{view.fullname}}     <br />     controller binding "foobinding": {{view.foo.firstname}} {{view.foo.lastname}} </script> 

(see fiddle)

another way this, set author property route via route#setupcontroller:

author = ember.object.extend     firstname: null     lastname: null         fullname: (->         author = @get 'foo'         "#{@.get('firstname')} #{@.get('lastname')}"     ).property 'firstname', 'lastname'  app.applicationroute = ember.route.extend      setupcontroller: (controller, model) ->         controller.set 'author', author.create             firstname: 'bill',             lastname: 'buckley' 

and templates can access author property directly, since it's in view's controller:

<script type="text/x-handlebars" data-template-name='author'>     written by: {{author.fullname}} </script> 

(see fiddle)

this way don't have set binding anywhere.

notes: create computed properties in object, might used not view, other objects using author instance , avoid initializing properties undefined.


to better use ember's capabilities, can define route author , set controller's content property author instance , add {{outlet}} template. framework find controller , connect template, again, using naming conventions:

handlebars

<script type="text/x-handlebars" data-template-name='author'>     written by: {{fullname}} </script>  <script type="text/x-handlebars">     {{outlet}} </script> 

coffee

window.app = app = ember.application.create()  app.router.map ->     @.route 'author', { path: '/'}  app.author = ember.object.extend     firstname: null     lastname: null         fullname: (->         "#{@.get('firstname')} #{@.get('lastname')}"     ).property 'firstname', 'lastname'  app.authorroute = ember.route.extend      setupcontroller: (controller, model) ->             # come api             controller.set 'content', app.author.create                 firstname: 'bill',                 lastname: 'buckley'  app.authorcontroller = ember.objectcontroller.extend() 

(see fiddle)


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -