2012-12-01 38 views
0

骨幹型號,主板:backbonejs model.toJSON()不能得到正確的輸出,與backbone.iobind

define([ 
    'underscore', 
    'backbone', 
    'collections/lists', 
    'iobind', 
    'iosync' 
], function(_, Backbone, Lists,ioBind,ioSync) { 

    var BoardModel = Backbone.Model.extend({ 
    urlRoot: 'board', 
    noIoBind: false, 
    socket: io.connect(''), 
    idAttribute: '_id', 

    defaults: { 
     title: 'One Thousand and One Nights' 
    }, 

    initialize: function() { 
     this.id = 1; 
     this.lists = new Lists; 
     this.socket.emit('joinBoard',this.id); 

     _.bindAll(this, 'getBoard'); 
     this.ioBind('initBoard', this.getBoard, this); 
    }, 

    getBoard: function(data){ 
     this.set(data.data.board[0]); 
    } 
    }); 

    return BoardModel; 
}); 

骨幹查看:boardView:

在取景功能
var IndexView = Backbone.View.extend({ 

    // Instead of generating a new element, bind to the existing elements in the HTML. 
    el: '#board', 

    // Board template html 
    template: Mustache.render(Template.board), 

    events: { 

    }, 

    initialize: function() { 
     //Init Data 
     this.model = new Board(); 
//  var lists = { 
//  lists: [ 
//   {name: "To Do", 
//   cards:[ 
//    {name: "Art work for A."}, 
//    {name: "B Prototype."}, 
//    {name: "C prototype."} 
//   ] 
//   }, 
//   {name: "Doing", 
//   cards: [ 
//    {name: "Art work for A."} 
//   ] 
//   }, 
//   {name: "Done"} 
//  ] 
//  } 
//  var partial = {card: Template.card_in_list}; 
//  var listHtml = Mustache.render(Template.list,lists,partial); 
//  template = $(this.template).find('.list-area').append(listHtml); 
    }, 

    render: function() { 

     console.log(this.model); 
     console.log(this.model.toJSON()); 

     var partial = {card: Template.card_in_list}; 
     var listHtml = Mustache.render(Template.list,this.model,partial); 
     template = $(this.template).find('.list-area').append(listHtml); 
     this.$el.html(template); 
    } 

    }); 

:渲染功能,console.log得到不同的結果。 的console.log(this.model)可以得到正確的對象結果:

child 
_callbacks: Object 
_changing: false 
_escapedAttributes: Object 
_ioEvents: Object 
_pending: Object 
_previousAttributes: Object 
_silent: Object 
attributes: Object 
__v: 0 
_id: "50b750a7795f285d4e000014" 
created: "2012-11-29T12:10:15.269Z" 
description: "simple is better, but not simpler" 
dueDate: "2012-11-29T12:10:15.269Z" 
lists: Array[6] 
status: true 
title: "test board unique" 
__proto__: Object 
changed: Object 
cid: "c1" 
getBoard: function() { [native code] } 
id: "50b750a7795f285d4e000014" 
lists: child 
__proto__: ctor 

但this.model.toJSON()只得到模型的默認值:

Object 
title: "One Thousand and One Nights" 
__proto__: Object 

它讓我困惑。任何人都知道爲什麼相同的模型會得到不同的結果

+0

嗨!我看到你在這裏問的是https://github.com/logicalparadox/backbone.iobind/issues/26你是如何解決「未捕獲的ReferenceError:模塊沒有定義」的問題的?乾杯。 – Puigcerber

回答

0

我發現我觸發boardView.render兩次。當我改變代碼:

a = new boardView; 
a.render(); 

a = new boardView; 

我得到了要做的事情。

順便感謝Marcel Falliere的評論。

0

在骨幹模型中,您的業務值(描述,標題...)存儲在attributes屬性中。當您在模型上調用toJSON()時,它所做的是取attributes值,並刪除Backbone.Model對象框架的函數和屬性。

當您手動設置模型屬性時,您想使用set。我不知道什麼是你data.data對象,所以你應該檢查文檔:http://backbonejs.org/#Model-set

setmodel.set(attributes, [options])

Set a hash of attributes (one or many) on the model. If any of the attributes change the models state, a "change" event will be triggered, unless {silent: true} is passed as an option. Change events for specific attributes are also triggered, and you can bind to those as well, for example: change:title, and change:content. You may also pass individual keys and values.

note.set({title: "March 20", content: "In his eyes she eclipses..."});

book.set("title", "A Scandal in Bohemia"); If the model has a validate method, it will be validated before the attributes are set, no changes will occur if the validation fails, and set will return false. Otherwise, set returns a reference to the model. You may also pass an error callback in the options, which will be invoked instead of triggering an "error" event, should validation fail. If {silent: true} is passed as an option, the validation is deferred until the next change.

+0

我已經成功地獲得了正確的屬性,但在我的view.render函數中,我使用console.log來輸出我的屬性,它顯示正確,但是當我toJSON屬性時,它直接給我模型的默認值,這讓我非常困惑。你有什麼意見嗎? – xds2000

+0

否您的模型是錯誤地構建的。爲什麼?因爲我看到有'description','dueDate','lists','status'和MOREOVER,所以'title'屬性不是你指定的默認值。看看它:你指定的默認值是在yourmodel.attributes.title !!據我所知,我的評論是正確的。您的模型屬性設置錯誤... –