2012-12-01 3 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 "Uncaught ReferenceError : module is not defined"문제를 어떻게 해결할 수 있었습니까? 건배. – 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에있다 !! 그래서 제가 말할 수있는 한, 제 의견은 옳습니다. 모델 속성이 잘못 설정되었습니다 ... –