javascript - 具有 Backbone 模型的非 RESTful HTTP POST?

标签 javascript ajax json backbone.js

我一直在查看此文档:

http://backbonejs.org/#Sync

http://addyosmani.github.io/backbone-fundamentals/#backbones-sync-api

尝试弄清楚如何发出 HTTP POST 请求来获取一些 JSON 数据以加载到我的模型中。遗憾的是,Web 服务不是 RESTful,因此我正在执行 POST 请求,而实际上它应该是 GET 请求。生活就是这样……

我最终得到了以下结果,但我不确定这是否是正确的方法,或者是否有更简单的方法,而且因为我没有严格使用 REST 模式,所以我似乎找不到任何联系我的服务采用 Backbone 模式。

    define([
    'underscore',
    'backbone',
], function (_, Backbone)
{
    'use strict';

    //model class declaration
    var LocationsModel = Backbone.Model.extend({

        locations : null, //this stores the json data of buildings and locations

        //url for http post to get location data
        url: '/locations',

        //http request settings
        requestType: 'POST',
        requestContent : 'application/json; charset=utf-8',
        requestBody: { "lastRefreshedDateTime": "2015-04-01T08:18:06.000+00:00", "uid": "12356789" },
        requestData: 'json',

        /**
         @name constructor
         @method initialise
        **/
        initialize: function (xhr)
        {
            this.doPOST();
        },

        /**
         @name doPOST
         @method doPOST
         @summary uses AJAX HTTP POST to fetch data from EAS
        **/

        doPOST: function ()
        {
            $.ajax({
                type: this.requestType,
                data: this.requestBody,
                contentType: this.requestContent,
                dataType: this.requestData,
                url: "http://" + window.location.host + this.url,
                success: function (result)
                {
                    this.locations = result;
                },
                error: function (jqXHR, textStatus, errorThrown)
                {
                    //TODO - load cached JSON of buildings and locations
                },
            });
        }


    });

    return LocationsModel;

});

所以这段代码本质上是

  1. 使用 header 信息构建 POST 请求
  2. 提出请求
  3. 如果成功,请将 JSON 响应加载到模型中

我是否可以对此进行更多抽象?

最佳答案

覆盖.fetch():

var RestlessModel = Backbone.Model.extend({
    defaults: function(){
        return {
            locations: []
        }
    },
    //url for http post to get location data
    url: '/locations',

    //http request settings
    fetchOverride: {
        type: 'POST',
        contentType : 'application/json; charset=utf-8',
        data: JSON.stringify({ "lastRefreshedDateTime": "2015-04-01T08:18:06.000+00:00", "uid": "12356789" }),
        dataType: 'json',        
    },
    fetch: function(options){
        options = _.extend(options || {}, this.fetchOverride);
        Backbone.Model.prototype.fetch.call(this, options);
    }

});

See fiddle ^^

如果您需要处理请求响应,请覆盖.parse()

关于javascript - 具有 Backbone 模型的非 RESTful HTTP POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30373315/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property 'abort' of undefined

javascript - 使用 javascript 捕获文档级按键事件

java - 处理与 Jackson `JsonIgnoreProperties` 的 JPA/Hibernate 实体双向关系的循环引用/依赖关系时出错

python - Django (1.7) - 数据迁移

javascript - 当用户单击模态触发器时,模态未打开

php - 如何将 php 值传递给 javascript?

javascript - ngAnimate 破坏了现有的 ui.bootstrap.carousel

php - 将ajax中的变量从一个页面传递到另一个页面

javascript - 如何将 json 对象插入 Google 图表线

javascript - Famo.us Scrollview + TouchSync - 使 ScrollView/TouchSync 意识到旋转 Z 变换