我正在尝试实现路由器事件并使用路由器的send功能来触发路由器上的事件。但无法得到任何关于此的文件。
我想要实现的是我从控制器/视图中提出一个事件来从服务器获取数据。并且事件从服务器异步获取数据,并且在成功获取数据时,我想从我调用事件的位置初始化视图的子视图,即我需要知道何时获取数据。但我不认为路由器上的事件会返回任何内容,以便我可以知道呼叫何时结束
类似的东西:
查看:
Em.View.extend({
click: function(){
var recordsPromise = this.get('controller.target').send('getRecords');
recordsPromise.done(this.initiateProtocol);
},
showChild: false,
initiateProtocol: function(){
//this showChild is used as a condition in the template to show/hide
// the childview. And is being set after the call completed
// successfully
//here childOneView is present some other js file and is fetched using requirejs
require(['childOneView'], $.proxy(function(childOneView){
this.set('childOne', childOneView.extend({
templateName: 'childOne'
});
this.set('showChild', true);
}, this));
}
}
路由器
Em.Route.extend({
events: {
getRecords: function(){
//make an ajax call to fetch the records and return the ajax as a
// promise
}
}
});
模板
{{#if view.showChild}}
{{view view.childOne}}
{{/if}}
答案 0 :(得分:0)
我认为惯用的Ember方法会有所不同。将操作发送到控制器并让它冒泡到路径,然后设置视图将通过绑定响应的属性:
查看强>
App.RecordsView = Em.View.extend(Ember.ViewTargetActionSupport, {
click: function(){
this.triggerAction({action: 'getRecords'})
}
});
<强>控制器强>
App.RecordsController = Em.ArrayController.extend({
content: [],
isLoaded: false
});
<强>模板强>
<!-- records.handlebars -->
{{#if isLoaded}}
render stuff here... perhaps {{#each this}}{{someRecordProperty}}{{/each}}
{{/if}}
<强>路由器强>
App.RecordsRoute = Em.Route.extend({
events: {
getRecords: function(){
var controller = this.controllerFor('records');
$.ajax(...).then(function(data){
Em.run(function(){
controller.setProperties({
content: data.foo,
isLoaded: true
});
});
});
}
}
});