CanJS如何刷新模型 [英] CanJS how to refresh a model
本文介绍了CanJS如何刷新模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个模型,代表一个在服务器上运行的作业列表
I have a model which represents a list of jobs which are run on the server
我想轮询服务器上的计时器更新,以显示对服务器的更改。工作状态。
I want to poll the server for updates on a timer to show changes to the state of the jobs.
我该怎么做?
我的控制看起来像这样
var control = Control({
defaults: {
view: 'app/views/job-index.ejs'
}
}, {
init: function () {
this.element
.empty()
.append(can.view(this.options.view, this.options));
var options = this.options;
window.setInterval(function() {
options.result.refresh();
}, 1000);
},
});
我的模型,到目前为止看起来像这样
my model, so far looks like this
var model = can.Model({
findOne: 'GET /api/jobs'
}, {
refresh: function() {
// what goes here?
}
});
推荐答案
首先观察一下:
如果要获取工作集合,则可能应该使用 findAll
而不是findOne:
If you are getting a collection of jobs you should probably want to use findAll
instead of findOne:
findAll: 'GET /api/jobs',
findOne: 'GET /api/jobs/{id}'
我知道结果
是一条记录。因此,您可以执行以下操作:
I understand that result
is a single record. So you can do something like:
var Model = can.Model({
findAll: 'GET /api/jobs',
findOne: 'GET /api/jobs/{id}'
}, {
refresh: function () {
var id = this.attr('id');
var self = this;
Model.findOne({id: id}, function (model) {
self.attr(model.attr());
});
}
});
此外,按照惯例,您应该将模型类命名为 Model
不是模型
。
Also, by convention you should name your model class Model
not model
.
这里是一个小提琴 http://jsbin.com/xarodoqo/4/edit
这篇关于CanJS如何刷新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文