【问题标题】:Model destroy calling incorrect REST url模型销毁调用不正确的 REST url
【发布时间】:2014-11-19 12:31:17
【问题描述】:

我的模特:

var Backbone = require('backbone');

module.exports = Backbone.Model.extend({
    idAttribute: '_id',
    defaults: {
        id: 0,
        description: '',    
        amount: 0,
        dateEntry: new Date(),
        positive: false
    },
    url: '/api/transactions'
});

我的收藏:

var Backbone    = require('backbone');
var Transaction = require('../models/Transaction')

var TransactionCollection = Backbone.Collection.extend({

    model: Transaction,

    url: '/api/transactions',

    initialize: function() {
        this.hasNextPage = false;
        this.filter = 'all';
    },

    parse: function(response) {
        this.hasNextPage = response.hasNextPage;
        this.balance = response.balance;
        return response.transactions || [];
    }

});

module.exports = new TransactionCollection();

我有许多项目视图,每个视图都显示其模型数据(交易)。

在每个项目视图中都有一个“删除”按钮触发:

delete: function() {
        console.log("Remove:: ");
        console.log(this.model.toJSON());
        this.model.destroy();
    }

查看控制台,我看到了对 /api/transactions/ 的 REST DELETE 调用

不应该是 /api/transaction/model_id 吗?实际上,我在服务器端(NodeJS)看到了空的 req.body 和 req.query。

浏览器控制台日志:

[Log] Object (app.js, line 20298)
__v: 0
_id: "5421da84c6fd7c91060ba405"
amount: 1200
category: Object
dateAdded: "2014-09-23T20:39:32.905Z"
dateEntry: "2014-06-17T22:00:00.000Z"
description: "Example!"
id: 0
positive: true
user: "53fbb34fb91a922f03be61f8"
__proto__: Object

交易数据来自 MongoDB,因此具有 _id 属性。

我怀疑这是我的问题的原因。如何让我的 Backbone 应用调用正确的 DELETE url?

【问题讨论】:

标签: javascript node.js mongodb rest backbone.js


【解决方案1】:

在您的模型中,当您应该覆盖 urlRoot 属性时,您将覆盖 url 属性

//intercept the request for testing.
$.mockjax({
  // matches /api/transactions/abCD1234
  url: /^\/api\/transactions\/([0-9a-zA-A]+)$/,
  urlParams: ['id'],
  response: function (settings) {
    var id = settings.urlParams.id;
    $('body').append(id); 
  }
});

var yourModel = Backbone.Model.extend({
    idAttribute: '_id',
    defaults: {
        id: 0,
        description: '',    
        amount: 0,
        dateEntry: new Date(),
        positive: false
    },
    //This is the property that sets the "base" of all your REST urls
    urlRoot: '/api/transactions'
});

var yourInstance = new yourModel({
  _id:'5421da84c6fd7c91060ba405',
  amount: 1200,
  dateAdded: "2014-09-23T20:39:32.905Z",
  dateEntry: "2014-06-17T22:00:00.000Z",
  description: "Example!",
  id: 0,
  positive: true,
  user: "53fbb34fb91a922f03be61f8"
}); 

yourInstance.destroy(); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.3/jquery.mockjax.min.js"></script>

【讨论】:

  • TY!顺便说一句,我需要在集合中保留一个 url 以供传统使用,并在模型中保留一个 urlRoot,因为我需要 model.save()
猜你喜欢
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2018-05-01
  • 2019-11-03
相关资源
最近更新 更多