Ember.js 灰烬视图渲染太早
我有一个创建新文档、复制现有文档的方法Ember.js 灰烬视图渲染太早,ember.js,Ember.js,我有一个创建新文档、复制现有文档的方法 App.DocumentsNewRoute = Ember.Route.extend({ model: function (params) { this.modelParams = params; // save params for reference return App.Document.createRecord(); }, setupController: function (controller, model) {
App.DocumentsNewRoute = Ember.Route.extend({
model: function (params) {
this.modelParams = params; // save params for reference
return App.Document.createRecord();
},
setupController: function (controller, model) {
// use the params to get our reference document
var documentModel = App.Document.find(this.modelParams.document_id);
documentModel.one('didLoad', function () {
// once loaded, make a serialized copy
var documentObj = documentModel.serialize();
// and set the properties to our empty record
model.setProperties(documentObj);
console.log('didLoad');
});
}
});
我在视图中添加了一些日志
App.DocumentView = Ember.View.extend({
init: function () {
this._super();
// fires before the didLoad in the router
console.log('init view');
},
willInsertElement: function () {
// fires before the didLoad in the router
console.log('insert elem');
}
});
这是我的模板
{{#if model.isLoaded }}
{{ view App.DocumentView templateNameBinding="model.slug" class="document portrait" }}
{{ else }}
Loading...
{{/if}}
问题似乎是我的模型被加载了
,但在呈现模板时没有填充,因此templateNameBinding此时不存在,并且在填充数据时似乎没有更新
我应该使用模板中加载的model.isLoaded以外的其他内容,还是应该强制重新呈现模板,如果是,如何以及在何处?谢谢 看来你把事情复杂化了。应该是:
编辑
我在一读时误解了你的问题,所以
App.DocumentsNewRoute = Ember.Route.extend({
model: function (params) {
var originalDoc = App.Document.find(params.document_id),
newDoc = App.Document.createRecord();
originalDoc.one('didLoad', function () {
newDoc.setProperties(this.serialize());
});
return newDoc;
},
setupController: function (controller, model) {
controller.set('content', model);
}
});
如果一切正常,余烬将自动重新渲染东西。啊,一开始它不起作用。但是,当我在模板中将{{{if model.isLoaded}更改为{{{if model.slug}}时,我的templateNameBinding开始工作。谢谢