Skip to content

Instantly share code, notes, and snippets.

@gutenye
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gutenye/9824552 to your computer and use it in GitHub Desktop.
Save gutenye/9824552 to your computer and use it in GitHub Desktop.
ember basic
App.IndexRoute = Ember.Route.extend({
  model: function() {
    return Ember.Object.create({name: 1})
  },
  
  setupController: function(controller, model) {        // 他省略了setupController
    controller.set('model', model)                     // 这里的model就是上面的 Ember.Object.create({name: 1})
    controller.set('age', 2)                           // 模板里面 {{age}}
  }
})


App.IndexController = Ember.ObjectController.extend({    // 这里用了ObjectController 不是Controller
  address: 'bar'                                         // 模板里面 {{address}}
})

ObjectController的作用是 所有没有的属性都自动代理到model上面去. 比如

{{age}}   // 2 controller里面已经有了
{{name}}   // 1 controller里面没有name属性呀, 代理到{{model.name}}
{{model.name}} // 1   和上面一样的

进一步

App.IndexController = Ember.ObjectController.extend({
  nameDisplay: function() {
    return 'Hi, ' + this.get('name')           // Hi, 1      this.get('name') controller里面没有name属性呀, 代理到{{model.name}}
  }.property('name')
})

require

这要看你require的是什么东西了

// a.js
exports = Date;

// b.js
var Date = require('./a');
new Date();

//---------

// a.js
exports = 1;

// b.js
var one = require('./a');
console.log(one);       // -> 1

//---------

// a.js
exports = function() { console.log(2) };

// b.js
var log = require('./a');
log();               // -> 2
@gutenye
Copy link
Author

gutenye commented Mar 28, 2014

Controller 的作用就是连接 Model + Template (像中间层)
默认的setupController是

setupController: function(controller, model) {
  controller.set('model', model)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment