简体   繁体   English

骨干模型保存不起作用

[英]backbone model save not working

I tried to use Backbone.js model to sync data with the server. 我尝试使用Backbone.js模型与服务器同步数据。 However the save method doesn't work. 但是,保存方法不起作用。 Any one can help me to find out why? 任何人都可以帮助我找出原因吗?

The following code works (not using Backbone model): 以下代码有效(不使用Backbone模型):

var newComment = {
    user: user,
    createdDate: date,
    content: text
  }

  //TODO send ajax post request to record the data

  $.ajax({
    url: "comment.php",
    context: document.body,
    data: newComment,
    type: 'POST'
  }).done(function(resp) {
    console.log("resp", resp);
  });

The code not working: 代码不起作用:

var Comment = Backbone.Model.extend({
     urlRoot: 'comment.php'
  });

var comment = new Comment();
comment.save({content: text, dsm_line_item_id: "49934380"}, {
    succcess: function(model, resp, opt) {
          console.log("successfully saved" + resp);
        },
    error: function(model, resp, opt) {
          console.log("error", resp);
        }
    })
succcess: function(model, resp, opt) {

is this a typo 这是错字吗

Supposed to be success: function(model, resp, opt) { 假设success: function(model, resp, opt) {

This is a guess, but I think that it might be the answer. 这是一个猜测,但我认为可能是答案。 Backbone's save delegates to Backbone.sync , the function that Backbone uses to read or save models to the server. Backbone的save委托到Backbone.sync ,Backbone使用此功能将模型读取或保存到服务器。

Backbone uses POST, GET, DELETE and PUT; 骨干网使用POST,GET,DELETE和PUT; but some servers have problems with PUT and DELETE. 但是某些服务器在PUT和DELETE方面存在问题。 Maybe your server has a problem with PUT and that's why your code isn't working. 也许您的服务器存在PUT问题,这就是为什么您的代码无法正常工作的原因。

You could try with Backbone.emulateHTTP = true; 您可以尝试使用Backbone.emulateHTTP = true; . That option allows you to work with legacy web servers that don't support Backbone's RESTful approach. 该选项允许您使用不支持Backbone的RESTful方法的旧式Web服务器。 The PUTs and DELETEs will be subsituted with POSTs during the sync. 在同步过程中,PUT和DELETE将替换为POST。

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

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