0
我是一般的新canJs。我很难理解can.fixture和can.observe之间的区别以及它们是如何被使用的?can.fixture和can.observe之间有什么区别,它们在哪里可以使用?
据我了解,can.observe只是监听一个函数并持续观察它,它需要两个参数。至于夹具,我真的失去了,因为我一直听到偷窃的消息。
我是一般的新canJs。我很难理解can.fixture和can.observe之间的区别以及它们是如何被使用的?can.fixture和can.observe之间有什么区别,它们在哪里可以使用?
据我了解,can.observe只是监听一个函数并持续观察它,它需要两个参数。至于夹具,我真的失去了,因为我一直听到偷窃的消息。
他们有很大的不同:
观察,在那里你可以得到每当值改变事件观察对象:
var ob = new can.Observe({
name: 'Test'
});
ob.bind('change', function(ev, attr, how, newVal, oldVal) {
console.log('Something on ob changed, the new value is: ' + newVal);
});
ob.attr('name', 'Changed');
这可以在很多地方是有用的,使视图绑定是可能的(所以当使用Observe渲染视图时,只要您自动更新Observe,您的HTML就会更改)。阅读更多关于它in the documentation。
夹具只是模仿AJAX响应,可以帮助你测试你的应用程序或实现 功能,即使你休息后端不起来,但运行:
// Static fixture
can.fixture("tasks", "fixtures/tasks.json");
can.ajax({
url: 'tasks',
dataType: 'json'
}).done(function(data) {
// data is the content of fixtures/tasks.json
// instead of making a request to tasks
});
// Dynamic fixture
can.fixture("/foobar.json", function(original, response){
response(200, "success", { json: {foo: "bar" } }, {})
});
如果双方走到一起是在使用Models时。模型基本上只是从REST后端获取数据的观察。 More on Models and fixtures。
什么是你在can.observe中提到的ob.bind? – copthen
您需要的所有信息应在入门指南和文档中:http://canjs.com/docs/can.Observe.prototype.bind.html – Daff