它是O.K.在值硬编码重新写Backbone.sync? [英] Is it O.K. to re-write Backbone.sync by hard coding in values?

查看:91
本文介绍了它是O.K.在值硬编码重新写Backbone.sync?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个特定的情况下,我硬codeD的方法来创建或POST还同时设置的emulate选项设置为false。按照这个逻辑之后,我得到这个更短的方法:

  //硬code在仿真和方法
  Backbone.sync =功能(方法,模型,期权){
    选择|| (选项= {});
    VAR PARAMS = {类型:POST,数据类型:'JSON'};    //确保我们有一个网址。
    如果(!options.url){
      params.url = _.result(型号,URL)|| urlError();
    }    //确保我们有适当的请求数据。
    如果(options.data == NULL){
      params.contentType ='应用/ JSON';
      params.data = JSON.stringify(options.attrs || model.toJSON(选项));
    }
    params.processData = FALSE;    VAR XHR = options.xhr = Backbone.ajax(_延伸(参数,可以选择)。);
    model.trigger('要求',型号,XHR,期权);
    返回XHR;
  };


解决方案

一般来说,硬编码公认的答案是做这样的方式,使您的方法,可重复使用。你基本上试图使工作少为自己的道路。

在这种情况下,就像POST和JSON部分都很好。你看到自己打算从POST到GET改变方法?你打算写一个GET方法?然后使它的参数,或可配置的选项。 (我很怀疑你的情况,但我只是使用为例)

在获取数据和所有的方面,你似乎是确定的测试。 (因为你只想让你的脚离开地面)。只记得在方法switch语句添加为每个不同的数据的情况下...

编辑:此方法交换机上创建的第一个参数,它定义了一种操作正在对数据做了什么switch语句。下面是一个例子:

  Backbone.sync =功能(方法,模型,期权){
    选择|| (选项= {});
    VAR PARAMS = {类型:POST,数据类型:'JSON'};
    开关(方法){
       情况下创造:
       //让我们去使数据(POST)
       ...
       打破;       案更新:
       //让我们去更新数据
       ...
       打破;       情况下删除:
       //我们删除一些数据
       ...
       打破;       案读:
       //让我们去收集我们的数据(GET)
       ...
       打破;
   }
   返回XHR;
};

In this particular case I hard-coded in the method to create or POST and also set both the emulate options to false. After applying this logic I got this much shorter method:

  // hard code in the emulation and method
  Backbone.sync = function(method, model, options) {
    options || (options = {});
    var params = {type: 'POST', dataType: 'json'};

    // Ensure that we have a URL.
    if (!options.url) {
      params.url = _.result(model, 'url') || urlError();
    }

    // Ensure that we have the appropriate request data.
    if (options.data == null) {
      params.contentType = 'application/json';
      params.data = JSON.stringify(options.attrs || model.toJSON(options));
    }
    params.processData = false;

    var xhr = options.xhr = Backbone.ajax(_.extend(params, options));
    model.trigger('request', model, xhr, options);
    return xhr;
  };

解决方案

Generally, the accepted answer for hardcoding is do it in such a way that makes your method reusable. You're essentially trying to make less work for yourself down the road.

In this case, sections like having POST and JSON are fine. Do you see yourself planning to change the method from a POST to a GET? Do you plan on writing a GET method? Then make it a parameter, or a configurable option. (I highly doubt in your case, but I'm just using that as an example)

In terms of getting data and all that, you seem to be ok as testing. (since you just wanna get your feet off the ground). Just remember to add in the method switch statement for each of the different data cases...

Edit: The method switch is creating a switch statement on the first parameter, which defines what kind of operation is being done on the data. Here is an example:

Backbone.sync = function(method, model, options) {
    options || (options = {});
    var params = {type: 'POST', dataType: 'json'};
    switch (method) {
       case 'create':           
       //Let's go make data (POST)
       ...
       break;

       case 'update':
       //let's go update our data
       ...
       break;

       case 'delete':
       //Let's remove some data
       ...
       break;

       case 'read':
       //let's go gather our data (GET)
       ...
       break;
   }
   return xhr;
};

这篇关于它是O.K.在值硬编码重新写Backbone.sync?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆