2013-04-29 31 views
15

我有這樣的骨幹代碼,創建一個視圖和模型,並調用保存方法將數據保存到數據庫:骨幹節省模式的成功和錯誤

var form = document.forms[0]; 

var RetailerModel = Backbone.Model.extend({ 
    urlRoot: ' retailer.php', 
    defaults: { 
     name: 'company-name', 
     address: 'company-address', 
     phone: 'company-phone', 
     icon: 'http://localhost/icon.png' 
    } 
}); 

var RetailerCollection = Backbone.Collection.extend({ 

}); 

var RetailerView = Backbone.View.extend({ 

    className: 'retailer', 

    template: _.template($('#retailer-template').html()), 

    initialize: function() { 

     var obj = { 
      name: form.name.value, 
      address: form.address.value, 
      phone: form.phone.value 
     }; 

     var o = this; 

     this.model.save(obj, { 
      success: function(model, response) { 
       console.log(model); 
       console.log(response); 
       o.render(); 
       console.log('success'); 
      }, 
      error: function(model, response) { 
       console.log(model); 
      } 
     }); 
    }, 

    render: function() { 
     $('#retailer-list').append(this.$el.html(this.template(this.model.toJSON()))); 

     return this; 
    } 
}); 

var RetailerViews = Backbone.View.extend({ 

}); 

$('#submit').click(function(e){ 
    var retailer_model = new RetailerModel(); 
    var retailer_view = new RetailerView({model: retailer_model}); 
    form.reset(); 
}); 

和接收數據的PHP代碼如下:我有

<?php 
$connect = mysql_connect('127.0.0.1','root','xxxxxx'); 
if (!$connect) { 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("retail", $connect); 
if($_SERVER['REQUEST_METHOD'] == 'POST') //POST GET PUT DELETE 
{ 
    $data = json_decode(file_get_contents('php://input'), true); 
} 

$name  = $data['name']; 
$address = $data['address']; 
$phone  = $data['phone']; 
$icon  = $data['icon']; 

if(!(mysql_query("INSERT INTO retailer (name, address, phone, icon)VALUES ('".$name."', '".$address."','$phone', '".$icon."')"))) 
{ 

    echo 200; 
} 
else{ 
    echo 'record has not been insert to db'; 
} 

mysql_close($connect); 

?> 

的一個問題是,當誤差函數被調用時,模型從服務器仍然有修改的屬性返回。我想知道爲什麼會發生這種情況,我如何確保如果發生錯誤,模型保持不變。

另一個問題是在php代碼中,當sql查詢成功時,如果我回顯200或'200',主幹會調用成功,但如果我回顯一個字符串,主幹會調用錯誤,我想知道它背後的邏輯是什麼。

回答

27

backbone docs:

通行證{等待:真正}如果你想在模型上設置 新屬性之前等待服務器。

如果您不希望模型更新,直到保存成功後全部通過wait: true作爲選項。

this.model.save(obj, { 
     success: function(model, response) { 
      console.log(model); 
      console.log(response); 
      o.render(); 
      console.log('success'); 
     }, 
     error: function(model, response) { 
      console.log(model); 
     }, 
     wait: true // Add this 
    }); 
+1

+1的鏈接文檔 – 2013-07-16 02:52:24

7

的骨幹

save(so are others like fetch, update...) 

返回一個承諾。您可以使用

save().done( 
    function(data) {}, // success 
    function(err) {} // fail 
) 

就像您如何處理承諾一樣。 done()方法保證在服務器返回東西后執行

查看jQuery API docs for AJAX.jqXHR瞭解更多信息。

+0

是不是'.done'爲'.success'的代名詞?似乎你正在尋找'.always' http://api.jquery.com/jquery.ajax/ – KFunk 2017-09-26 00:11:27

2

Backbone返回一個承諾。

這是我得到它的作品。

save({wait: true}).success(function(data){ 
    console.log(data); //returned data from server 
}).error(function(error){ 
    console.log(error); //error returned from server 
});