0
votes

backbone Model,board:

 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;
});

backbone View: 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);
    }

  });

in View function: render function, the console.log get different result. console.log(this.model) can get correct object result:

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

but this.model.toJSON() only get model default values:

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

it confuse me. anyone know why reason the same model get different result.

2
Hi man! I saw you were asking here github.com/logicalparadox/backbone.iobind/issues/26 How did you manage to solve the "Uncaught ReferenceError: module is not defined" problem? Cheers.Puigcerber

2 Answers

0
votes

In a Backbone Model, your business values (description, title ...) are store in the attributes attribute. When you call toJSON() on your model, what it does is it takes the attributes values, and remove the Backbone.Model object framework's functions and attributes.

When you manually want to set model attributes, you want to use set. I don't know what is in you data.data object, so you should check the doc : http://backbonejs.org/#Model-set

set model.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
votes

I found i trigger boardView.render twice. when i change code:

a = new boardView;
a.render();

to

a = new boardView;

i got the thing done.

by the way thanks Marcel Falliere's comments.