有以下can.Model:
var CaseModel = can.Model.extend({
findAll: function(args){
var def = $.Deferred();
args.sobject.retrieve(args.criteria, function(err, records) {//records should be populated into view
if(err) def.reject(err);
else def.resolve(records);
});
return def;
}}, {});
我应该如何使用init方法实现can.Control以使用deffered = CaseModel.findAll({sobject:new SObjectModel.Case()}填充can.view,如下所示:
this.element.html(can.view('recipes',Deffered));
以及这些记录如何在小胡子模板中循环: {{#each ???? }}
由于
答案 0 :(得分:0)
以下是如何使用延迟执行此操作的示例:
var CaseModel = can.Model.extend({
findAll: function(args){
var def = $.Deferred();
setTimeout(function () {
console.log('time');
def.resolve([{name: 'sam'}]);
}, 1000);
return def.promise();
}}, {});
var Control = can.Control({
init: function (ele, options) {
var self = this;
var pro = CaseModel.findAll({});
can.view('view', {records: pro})
.then(function(frag) {
self.element.html(frag);
});
}
});
var control = new Control('.app');
<script type="text/mustache" id="view">
VIEW
<ul>
{{#each records}}
<li>
{{name}}
</li>
{{/each}}
</ul>
</script>
这是一个演示 http://jsbin.com/kedih/1/edit
您应该能够对此进行调整,以便在示例中包含args.sobject.retrieve
而不是setTimeout
。