骨干渲染位置

use*_*808 0 backbone.js

只是想知道为什么这样做:

window.NewListView = Backbone.View.extend({
  template: _.template('<a href="/list" class="button new-list">Create New List</a>'),

  render: function(){
    $(this.el).html(this.template());
    return this;
  }
});

window.List = new (Backbone.Router.extend({
  routes: { "": "index" },

  initialize: function(){
    this.newListView = new NewListView();
  },

  start: function(){
    Backbone.history.start();
  },

  index: function(){
    $('.lists').append(this.newListView.render().el);
  }
}));

$(function(){ List.start(); })
Run Code Online (Sandbox Code Playgroud)

而这不是:

window.NewListView = Backbone.View.extend({
  template: _.template('<a href="/list" class="button new-list">Create New List</a>'),

  render: function(){
    $(this.el).html(this.template());
    return this;
  }
});

window.List = new (Backbone.Router.extend({
  routes: { "": "index" },

  initialize: function(){
    this.newListView = new NewListView();
    $('.lists').append(this.newListView.render().el);
  },

  start: function(){
    Backbone.history.start();
  },

  index: function(){

  }
}));

$(function(){ List.start(); })
Run Code Online (Sandbox Code Playgroud)

差异只是移动

$('.lists').append(this.newListView.render().el);
Run Code Online (Sandbox Code Playgroud)

在路由器的initialize()和index()之间.

Edw*_*ith 5

这是因为你正在创建一个路由器的实例.

当你这样做时:

window.List = new (Backbone.Router.extend({...
Run Code Online (Sandbox Code Playgroud)

您正在加载DOM之前创建路由器的实例.因此,在您的initialize函数中,您的jQuery选择器不会返回任何节点.

如果你打开一个控制台,你可以在这个jsFiddle上看到记录到它的操作顺序:

http://jsfiddle.net/edwardmsmith/x64hw/

window.NewListView = Backbone.View.extend({
  template: _.template('<a href="/list" class="button new-list">Create New List</a>'),

  render: function(){
    $(this.el).html(this.template());
    return this;
  }
});

window.List = new (Backbone.Router.extend({
  routes: { "": "index" },

  initialize: function(){
    this.newListView = new NewListView();
    console.log("List Initialize");
    $('.lists').append(this.newListView.render().el);
  },

  start: function(){
    Backbone.history.start();
  },

  index: function(){

  }
}));

$(function(){ 
    console.log("Before List Start");
    List.start(); 
    console.log("After List Start");

})?
Run Code Online (Sandbox Code Playgroud)

结果是:

List Initialize
Before List Start
After List Start
Run Code Online (Sandbox Code Playgroud)

但是,如果在DOM加载后创建路由器实例:

window.NewListView = Backbone.View.extend({
  template: _.template('<a href="/list" class="button new-list">Create New List</a>'),

  render: function(){
    $(this.el).html(this.template());
    return this;
  }
});

window.List = Backbone.Router.extend({
  routes: { "": "index" },

  initialize: function(){
    this.newListView = new NewListView();
    console.log("List Initialize");
    $('.lists').append(this.newListView.render().el);
  },

  start: function(){
    Backbone.history.start();
  },

  index: function(){

  }
});

$(function(){ 
    console.log("Before List Start");
    list = new List();                
    list.start(); 
    console.log("After List Start");

})?
Run Code Online (Sandbox Code Playgroud)

订单如您所料,它的工作原理如下:

Before List Start
List Initialize
After List Start
Run Code Online (Sandbox Code Playgroud)

如这个jsFiddle所示:

http://jsfiddle.net/edwardmsmith/eDWfh/