简体   繁体   English

Backbone.js范围

[英]Backbone.js Scope

Can somebody explain to me the ascending order of the alerts, and the item values of the model in the following piece of backbone.js code? 有人可以在下面的lobe.js代码段中向我解释警报的升序以及模型的项目值吗?

var model = new Ingredient({"item" : "Before",});
alert("1");
alert(model.get('item')); // Before

model.fetch({ success: function() {  
  alert("3");
  alert(model.get('item')); // After
}});

alert("2");
alert(model.get('item')); // Before

I can't seem to figure out how to update the state of the model in the same scope which it was defined. 我似乎无法弄清楚如何在定义的相同范围内更新模型的状态。 Is that important? 那重要吗?

It's possible I'm thinking about this the wrong way, or I don't understand something fundamental about javascript scoping or functions. 我可能在想这是错误的方式,或者我不了解有关javascript作用域定义或函数的基本知识。

Thanks 谢谢

The success: function() is called asynchronously as it is really just a wrapper around a JQuery AJAX call. 成功:function()被异步调用,因为它实际上只是JQuery AJAX调用的包装。 In human speak -. 用人类的话来说。 The fetch method makes a request to the server for the model data. 提取方法向服务器请求模型数据。 The fetch method returns immediately and doesn't wait for the http request to complete. fetch方法立即返回,并且不等待http请求完成。 When the http request completes ( if successfully ) then the success: function() callback is called. 当http请求完成时(如果成功),则调用success:function()回调。 This will be the last thing that happens. 这将是最后发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM