2011-08-24 109 views
0

你好我有一個節省了主幹模型從PHP獲取返回值與Backbone.js的

public function generate() 
    { 
     //Converts JSON object to array 
     $data = decode(TRUE); 
     $this->load->library('db_models',$config=array('localhost', 'user', '343boys')); 
     $data = $this->db_models->getFields($data['database'],$data['table']); 
     //Converts $data to JSON object 
     $data = encode($data); 
     //Problem is here how to i assign this data back to my model. 
     return $data; 

    } 

我創建了一個JSON助手解碼和服務器下面的CodeIgniter控制器的php代碼編碼,其將JSON分別將數組和數組分配給JSON。這沒有問題,因爲我用螢火蟲測試過它們。 保存時代碼可以正常工作,但當我嘗試檢索返回$ data時遇到了問題;功能完成保存後。

我的模型是像這樣

var Table = Backbone.Model.extend({ 
    defaults:{ 
     'table':'mine', 
     'database':'db' 
}, 
     urlRoot : '/campusfeed/index.php/welcome/generate' 

    }); 
model.save(); 

因此,如何將我去通過函數保存價值和檢索哪些函數返回不是笨neccessarilt。

回答

0

有幾種選擇。

model = new Table({ 
    table: 'yours' 
    ,database: 'database' 
}); 

model.save(null, {success: success}); 

model = new Table(); 
model.set({ 
    table: 'yours' 
    ,database: 'database' 
}); 
model.save(null, {success: success}); 

model = new Table(); 
model.save(
    { 
    table: 'yours' 
    ,database: 'database' 
    } 
    ,{success: success} 
); 

傳遞給model.save()的第一個目的是要改變屬性的哈希值。第二個參數是選項散列。 success回調接收三個參數。更新後的模型,來自服務器的響應和xhr對象。成功的功能可以這樣定義:

function success(model, response, xhr) { 
    console.log('model saved', model, response, xhr); 
}