简体   繁体   English

将自定义方法添加到主干模型调用服务器,返回简单数据类型

[英]add a custom method to a backbone model calling server, returning a simple data type

Looking for some guidance on properly implementing custom logic on a model ("foo") using backbone. 寻找关于使用骨干在模型(“foo”)上正确实现自定义逻辑的一些指导。

We have a general requirement to avoid invocation direct .ajax calls (see below), and try to do everything consistently through the Backbone.js MV* framework. 我们有一般要求避免直接调用.ajax调用(见下文),并尝试通过Backbone.js MV *框架一致地执行所有操作。

I have implemented a method on the view (for lack of better place to put it), but I feel like this utility method would better be suited as a method on the model. 我已经在视图上实现了一个方法(因为没有更好的位置),但我觉得这个实用方法最适合作为模型的方法。 I've read articles suggesting implementation of over-riding the backbone .sync method to add custom methods on a model, but the trouble is, that I'm not sure it's "correct" to do so, when the model is expecting an object, the REST service method I'm actually invoking returns a simple boolean (true/false) value. 我读过一些文章,建议实现覆盖骨干.sync方法来在模型上添加自定义方法,但问题是,当模型期待一个对象时,我不确定它是“正确的” ,我实际调用的REST服务方法返回一个简单的布尔值(true / false)。

Here is the code I have implemented: 这是我实现的代码:

var myView = Backbone.View.extend({
             initialize: function (options) {
                  ...
             },
             canDelete: function (parmOne, parmTWo){
                         var okToDelete;
                         var url = '/url/to/check/if/delete/is/ok/parmOne/ParmTwo';
                           $.ajax({
                                   async: false,
                                   type: "GET",
                                   url: url,
                                   success: function (valBool, response, jqXHR) {
                                       okToDelete = valBool;

                                    },
                                    error: function (data, textStatus, jqXHR) {
                                       notifyError(data.responseText)
                                    }
                          });

                          return okToDelete;
                        }
            });

Looking for suggestions on implementation to make this cleaner? 寻找有关实施的建议,以使其更清洁?

Thanks 谢谢

You are right about not overwriting Backbone.sync . 你是不是要覆盖Backbone.sync As per the docs : 根据文档

By default, it uses jQuery.ajax to make a RESTful JSON request and returns a jqXHR. 默认情况下,它使用jQuery.ajax发出RESTful JSON请求并返回jqXHR。 You can override it in order to use a different persistence strategy, such as WebSockets, XML transport, or Local Storage. 您可以覆盖它以使用不同的持久性策略,例如WebSockets,XML传输或本地存储。

You'd override sync if you want to define a different peristence strategy . 如果要定义不同的持久性策略,则覆盖sync

Model-specific utility functions belong in the model Model-specific于模型的实用程序功能属于模型

For a utility function like yours the correct place to implement it would be on the model . 对于像你这样的实用功能,实现它的正确位置将在model

var myModel = Backbone.Model.extend({
  canDelete: function (parmOne, parmTWo){
    var url = '/url/to/check/if/delete/is/ok/parmOne/ParmTwo';
    return $.ajax({
      async: false,
      type: "GET",
       url: url
    });
  }
});

In your view, you'd probably have an even bound to a delete function, say, deleteModel . 在您的视图中,您可能甚至绑定了删除功能,例如deleteModel Since we wired the model.canDelete method to return the $.ajax promise we can do the error checking in the view, like this: 由于我们连接了model.canDelete方法以返回$.ajax promise,我们可以在视图中执行错误检查,如下所示:

var myView = = Backbone.View.extend({
  initialize: function (options) {
    ...
  },

  deleteModel: function(event) {
    var that = this;
    this.model.canDelete()
    .done(function (data) {
      // Do some stuff if ok to delete like:
      that.model.destroy();
    })
    .fail(function (data) {
      // Do some stuff if not ok to delete like:
      that.model.notifyError(data.responseText)
    });
  }
});

Of course you can keep your success/fail callbacks like you had them, if the model is only going to be used in a very limited manner. 当然,如果模型只是以非常有限的方式使用,你可以保留你的成功/失败回调。

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

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