模板不绑定Ember中的模型

时间:2013-12-19 12:36:09

标签: ember.js

我将尝试在ember中绑定模型,控制器和模板 这是我的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>

为什么模型没有在模板中绑定并且警报重新调整模型为空

是错误的

1 个答案:

答案 0 :(得分:2)

来自setupController的{​​{1}}和model方法未被调用。因为您的控制器是由TestingRoute视图助手创建的,而不是由过渡创建的。

例如使用以下作品:

<强>模板

render

<强>的javascript

<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>

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

此外,不推荐在控制器中使用操作,而是使用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")); } } }); 对象