2013-12-19 50 views
1

模型结合我会努力的模型,控制器和模板绑定在灰烬 这里是我的js模板不是灰烬

App = Ember.Application.create({}); 

App.Person = Ember.Object.extend({ 
    firstName: "r", 
    lastName: "issa" 
}); 

App.TestingRoute = Ember.Route.extend({ 
    model: function() { 
     return App.Person.create(); 
    }, 
    setupController: function (controller, model) { 
     controller.set("model", model); 
    } 
}); 

App.TestingController = Ember.ObjectController.extend({ 
    submitAction: function() { 
     alert("My model is :" + this.get("model")); 
    } 
}); 

我的模板是:

<script type="text/x-handlebars" data-template-name="application"> 
    {{render testing}} 
</script> 

<script type="text/x-handlebars" data-template-name="testing"> 

    {{input valueBinding="model.firstName"}} 
    {{input valueBinding="model.lastName"}} 
    <button {{action submitAction target="controller"}} class="btn btn-success btn-lg">Pseudo Submit</button> 
    <p>{{model.firstName}} - {{model.lastName}}</p> 

</script> 

什么s为什么模型没有绑定模板和alert retunn模型为空

回答

2

您的setupControllermodel来自TestingRoute的方法未被调用。因为你的控制器是由render视图助手创建的,而不是通过转换。

例如使用了以下工作:

模板

<script type="text/x-handlebars" data-template-name="testing"> 

    {{input valueBinding="model.firstName"}} 
    {{input valueBinding="model.lastName"}} 
    <button {{action submitAction target="controller"}} class="btn btn-success btn-lg">Pseudo Submit</button> 
    <p>{{model.firstName}} - {{model.lastName}}</p> 

</script> 

的JavaScript

App = Ember.Application.create({}); 

App.Router.map(function() { 
    this.route('testing', { path: '/' }) 
}); 

App.Person = Ember.Object.extend({ 
    firstName: "r", 
    lastName: "issa" 
}); 

App.TestingRoute = Ember.Route.extend({ 
    model: function() { 
     return App.Person.create(); 
    }, 
    setupController: function (controller, model) { 
     debugger   
     controller.set("model", model); 
    } 
}); 

App.TestingController = Ember.ObjectController.extend({ 
    actions: { 
     submitAction: function() { 
      alert("My model is :" + this.get("model")); 
     } 
    }  
}); 

这里是小提琴http://jsfiddle.net/marciojunior/8DaE9/

此外,控制器中的操作的使用已被弃用,以支持使用actions: { ... }对象