2013-12-19 53 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: { ... }對象