繁体   English   中英

骨干:我的模型未传递ID。 它是一个评论系统。 我正在尝试将创建的评论发布到后端

[英]Backbone: My model doesn't pass the id. Its a commenting system. I am trying to post the created comment, to the backend

骨干:我正在尝试创建一个评论系统。 我的模型未传递ID。 我正在尝试将创建的评论发布到后端。 尝试创建时,将id值作为选项传递,但永远不会传递给集合中的选项。 部分代码如下:

this.collection.create({body:commentVal},({wait:true,id:_id}));

/* This is comments Model */
define([
  'backbone'
], function(Backbone){
  var CommentModel = Backbone.Model.extend({  
    defaults: {
        body: " "
    } 
  });
  //Returns the model for the module
  return CommentModel;
});

/* This is comments collection */

define([
  'jquery',
  'backbone',
  '../models/comment'
], function($, Backbone, CommentModel){

  var CommentCollection = Backbone.Collection.extend({
    model: CommentModel,

    url: function() {
      var urlRoot = '/api/v1/comment';
      return urlRoot + '/' + this.id;
    },

    initialize: function(attrs,options){
     this.id = options.id;
    }

  });

  return CommentCollection;  // We should never return collection instantiated.
});

这种方式应该为您工作:

var comments = new CommentCollection([],{id:_id });
            comments.fetch({
                success: function(){},
                error: function(){}
});

暂无
暂无

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

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