AJAX调用的支柱 - 此后渲染视图视图、支柱、此后、AJAX

2023-09-10 20:35:21 作者:春心動

我有一个手风琴样的用户界面,其中对于用户有几个节点单击并展开的。在这些节点的点击,我需要做一个Ajax调用我的一个服务器端programs.The这里的想法是拨打电话,并听取成功或失败,要么情况下,我认为需要在模型对象返回挑选并随后传递到下划线模板来正确显示的值。

I do have an accordion kind of UI, where for a user there are several nodes to click and expand. On the click of those Nodes,I need to make an Ajax call to one my server side programs.The idea here is make the call, and listen for success or failure and either case , return back with the Model Object that my view needs to pick and subsequently pass to the Underscore template to display the values appropriately.

没有写骨干Ajax调用之前,所以有兴趣的专家那里知道,对于一个给定所需要的应该是我的最佳策略。

Haven't written a BackBone Ajax call before, and so was interested knowing from experts there, for a given need what should be my best strategy.

您的答案是多少AP preciated。

Your answers are much appreciated.

沙拉德

推荐答案

您有2个选项它们基本上是等价的,取决于味道。使用回调:

You have 2 options which are basically equivalent and depend on taste. Using callbacks:

myModel.fetch({
  success: function(model, response, options){
    // create and display new view
  },
  error: function(model, response, options){
    // display error
  }
});

或者用deferreds是等效的:

Or the equivalent using deferreds:

myModel.fetch().done(function(){
  // create and display new view
}).fail(function(response){
  // display error
});