0

骨干型号,板:

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

  });

在 View function:render 函数中,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

它让我感到困惑。任何人都知道为什么相同的模型会得到不同的结果。

4

2 回答 2

0

在主干模型中,您的业务价值(描述、标题...)存储在attributes属性中。当您调用toJSON()您的模型时,它所做的是获取attributes值,并删除 Backbone.Model 对象框架的函数和属性。

当您想要手动设置模型属性时,您想要使用set. 我不知道你data.data反对的是什么,所以你应该检查文档:http ://backbonejs.org/#Model-set

model.set(attributes, [options])

在模型上设置属性的散列(一个或多个)。如果任何属性更改模型状态,将触发“更改”事件,除非 {silent: true} 作为选项传递。特定属性的更改事件也会被触发,您也可以绑定到这些事件,例如:change:title 和 change:content。您还可以传递单个键和值。

note.set({title: "March 20", content: "在他眼里她黯然失色..."});

book.set("title", "波西米亚丑闻"); 如果模型有 validate 方法,会在设置属性之前进行验证,如果验证失败则不会发生任何变化,set 会返回 false。否则,set 返回对模型的引用。您还可以在选项中传递一个错误回调,如果验证失败,它将被调用而不是触发“错误”事件。如果 {silent: true} 作为选项传递,验证将推迟到下一次更改。

于 2012-12-01T16:33:11.843 回答
0

我发现我触发了 boardView.render 两次。当我更改代码时:

a = new boardView;
a.render();

a = new boardView;

我把事情做好了。

顺便感谢 Marcel Falliere 的评论。

于 2012-12-09T14:08:13.517 回答