Ember Unbound & Belongsto

你说的曾经没有我的故事 提交于 2019-12-11 17:52:19

问题


I have no problem doing {{unbound title}} or

{{#each file}}
{{unbound filename}}
{{/each}}

on a model.

BUT, all belongsto object in ember is really problematic for me. None of ways below work

{{unbound location.address}}

and

{{with location}}
{{unbound address}}
{{/with}}

both of these two result in empty output


回答1:


At the time your model is being processed, any belongsTo relationships are not resolved yet. Since you're not binding, it can't retroactively update once that data is available either. I found this workaround yesterday, helping my solve my (similar) issues with belongsTo: https://github.com/emberjs/data/issues/1405

For existing records, you need to do something like the following in your route:

// your-route beforeModel: function() {   var self = this;   return
Em.RSVP.hash({
    firstBelongsTo: this.store.find('first-belongs-to'),
    secondBelongsTo: this.store.find('second-belongs-to')
    }).then(function (models) {
      self.controllerFor('this-route').setProperties(models);   });

And in your controller, be sure to declare the properties before setting them as Ember tries to throw then into content when they don't exist:

// your-controller App.MyController = Ember.Controller.extend({  
firstBelongsTo: null,   secondBelongsTo: null });

By returning a promise in the beforeModel hook, you are telling the route to resolve the promise BEFORE loading the model, which also mean before any rendering occurs. This gives your application time to load the data up front before binding it to the select boxes.



来源:https://stackoverflow.com/questions/25051833/ember-unbound-belongsto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!