2013-06-20 124 views
11

我有一個使用Backbone.js的基本應用程序,它不會進行PUT調用(更新模型)。從前端來看,我打電話給一個型號爲save的函數不會進行PUT調用;但是,如果我將其替換爲destroy,它確實會向後端發出DELETE調用。任何人有任何想法可能是什麼問題?沒有觸發PUT請求的函數是saveTask函數。Backbone.js - model.save()沒有發起PUT請求

App.Views.Task = Backbone.View.extend({ 
    template: _.template("<label>ID:</label><input type='text' id='taskId' name='id' value='<%= _id %>' disabled /><br><label>Title:</label><input type='text' id='title' name='title' value='<%= title %>' required/><br><label>Content:</label><input type='text' id='content' name='content' value='<%= content %>'/><br><button class='save'>Save</button>"), 
    events: { 
     "change input":"change", 
     "click .save":"saveTask" 
    }, 
    render: function(eventName){ 
     $(this.el).html(this.template(this.model.toJSON())); 
     //console.log(this.generateTemplate()); 
     return this; 
    }, 
    change: function(event){ 
     var target = event.target; 
     console.log('changing ' + target.id + ' from: ' + target.defaultValue + ' to: ' + target.value); 
     change[target.name] = target.value; 
     this.model.set(change);*/ 
    }, 
    saveTask: function(){ 
     this.model.set({ 
      title:$("#title").val(), 
      content:$("#content").val() 
     }); 
     if(this.model.isNew()){ 
      App.taskList.create(this.model); 
     } else { 
      this.model.save({}); 
     } 
    } 
}); 
+0

您使用的是什麼版本的Backbone?更新版本爲0.9.9的模型時遇到了一些問題。這可能是保存過程中的一個沉默錯誤,或者是您的骨幹版本的錯誤。 – mor

回答

18

如果你的模型是新的,那麼在你保存它的時候它會觸發一個post方法。 如果你的模型不是新的,而且你正在更新它,它會激發一個PUT。

如果這不適合你,這可能是因爲你的模型沒有id屬性,萬一你使用的id不同,例如taskID,那麼在你的模型中你必須設置idAttribute到taskID所以骨幹使用這個屬性作爲Id,一切都將是正常的。

這樣的:

var Task= Backbone.Model.extend({ 
    idAttribute: "taskId" 
}); 

這裏是鏈接到文件上Idattibute http://backbonejs.org/#Model-idAttribute

也是另一個問題可能是{}在保存調用 嘗試只

this.model.save(); 

而不是

this.model.save({}); 
+0

感謝您的回覆。我的模型有一個ID屬性,並且我設置了'idAttribute' - 以確保在我試圖保存時檢查模型的id('this.model.id')。它仍然不會激發PUT - 而'model.destroy()'可以正常工作。我錯過了什麼嗎? –

+0

爲什麼你在保存電話中有{}?我認爲它應該只是model.save();沒有空的對象字面量作爲參數。 –

+0

謝謝 - 這是一個錯字(以前試圖使用成功回調),但它似乎不相關(沒有區別)。模型正確更改(屬性更新),但不保存到服務器。 –

2

我相信模型總是希望選擇參數,也可能是回調

this.model.save(null, { 
    success: function (model, response) { 

     // 
    }, 
    error: function() { 
     // 
    } 
}); 

如果你看一下骨幹SRC,你會發現,太...

======

// Set a hash of model attributes, and sync the model to the server. 
// If the server returns an attributes hash that differs, the model's 
// state will be `set` again. 
save: function (key, val, options) { 
    var attrs, method, xhr, attributes = this.attributes; 

    // Handle both `"key", value` and `{key: value}` -style arguments. 
    if (key == null || typeof key === 'object') { 
     attrs = key; 
     options = val; 
    } else { 
     (attrs = {})[key] = val; 
    } 

    options = _.extend({ 
     validate: true 
    }, options); 

    // If we're not waiting and attributes exist, save acts as 
    // `set(attr).save(null, opts)` with validation. Otherwise, check if 
    // the model will be valid when the attributes, if any, are set. 
    if (attrs && !options.wait) { 
     if (!this.set(attrs, options)) return false; 
    } else { 
     if (!this._validate(attrs, options)) return false; 
    } 

    // Set temporary attributes if `{wait: true}`. 
    if (attrs && options.wait) { 
     this.attributes = _.extend({}, attributes, attrs); 
    } 

    // After a successful server-side save, the client is (optionally) 
    // updated with the server-side state. 
    if (options.parse === void 0) options.parse = true; 
    var model = this; 
    var success = options.success; 
    options.success = function (resp) { 
     // Ensure attributes are restored during synchronous saves. 
     model.attributes = attributes; 
     var serverAttrs = model.parse(resp, options); 
     if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs); 
     if (_.isObject(serverAttrs) && !model.set(serverAttrs, options)) { 
      return false; 
     } 
     if (success) success(model, resp, options); 
     model.trigger('sync', model, resp, options); 
    }; 
    wrapError(this, options); 

    method = this.isNew() ? 'create' : (options.patch ? 'patch' : 'update'); 
    if (method === 'patch') options.attrs = attrs; 
    xhr = this.sync(method, this, options); 

    // Restore attributes. 
    if (attrs && options.wait) this.attributes = attributes; 

    return xhr; 
}, 
2

在我的情況下,它失敗,因爲validations.As我保存它驗證了模型和我使用上市接口的集合中的所有屬性的模型並不要求所有的一模型的屬性。

我在Google面臨同樣的問題並進行了搜索,發現了您的問題並閱讀了解決方案和註釋。但是我意識到在更新的主幹規範中提到了model.save()在模型請求之前執行時,調用驗證,如果驗證成功,則繼續執行其他明智的失敗,這就是它在Chrome調試器網絡選項卡中未顯示任何網絡請求的原因。

我已經爲我面臨的案例編寫了解決方案,其他可能會面臨不同的問題。

0

骨幹的sync function是我最終使用的。你必須傳入'update'作爲第一個參數('method'參數)。

+0

很高興知道反對意見是什麼。對我和這個網站來說更有建設性:) – Stubbs