Model view controller ember.js使用模板中模型实例的值
我想在模板中使用模型实例中的值,在PHP中,我将执行以下操作:Model view controller ember.js使用模板中模型实例的值,model-view-controller,ember.js,Model View Controller,Ember.js,我想在模板中使用模型实例中的值,在PHP中,我将执行以下操作: <?php class User { private $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } $localuser = new User("jo
<?php
class User {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
$localuser = new User("john doe");
?>
<p>My name is: <?= $localuser->getName(); ?></p>
我的名字是:
但是我如何在Ember.js中做到这一点呢?还是我误解了Ember.js中的MVC模型
这是我创建localuser实例的Ember.js代码:
Example = Ember.Application.create();
Example.User = Example.Object.extend({
firstname: null,
lastname: null,
network: null,
fullName: function() {
return this.get('firstName') + " " + this.get('lastName');
}.property('firstName', 'lastName')
});
Example.LocalUser = Example.User.create({
firstname: "John",
lastname: "Doe"
});
Example.IndexRoute = Ember.Route.extend({
setupController: function(controller) {
},
renderTemplate: function() {
this.render("exampleTemplate");
}
});
and my HTML template (which doesn't work):
<script type="text/x-handlebars" data-template-name="exampleTemplate">
{{#model Example.LocalUser}}
{{fullName}}
{{/model}}
</script>
Example=Ember.Application.create();
Example.User=Example.Object.extend({
名字:空,
姓氏:空,
网络:空,
全名:函数(){
返回this.get('firstName')+“”+this.get('lastName');
}.property('firstName','lastName')
});
Example.LocalUser=Example.User.create({
名字:“约翰”,
姓:“Doe”
});
Example.IndexRoute=Ember.Route.extend({
设置控制器:功能(控制器){
},
renderTemplate:function(){
此.render(“示例模板”);
}
});
和我的HTML模板(不起作用):
{{{#model Example.LocalUser}
{{fullName}}
{{/model}}
要从Example.LocalUser中获取全名值,我需要在模板中放入什么内容才能显示?您必须执行与PHP中几乎完全相同的操作:创建对象的实例。你几乎是对的,但有一些错误:
Example.LocalUser=Example.User.create({…})
来创建实例,但这是Ember中的反模式。约定是在应用程序命名空间上保留类,如Example.IndexRoute
、Example.User
等,但从不保留实例。模型对象是实例,将其显示在视图中的最佳方法是使用Model
或setupController
挂钩设置控制器{{{{model}}…{{/model}
车把助手。您可以使用控制器提供的变量:{{model}
或仅使用{{controller}}
,如果您在控制器的路由中设置了它firstname
->firstname
:)Example = Ember.Application.create();
Example.User = Ember.Object.extend({
firstName: null,
lastName: null,
network: null,
fullName: function() {
return this.get('firstName') + " " + this.get('lastName');
}.property('firstName', 'lastName')
});
Example.IndexRoute = Ember.Route.extend({
model: function() {
var user = Example.User.create({
firstName: "John",
lastName: "Doe"
});
return user;
},
renderTemplate: function() {
this.render("exampleTemplate");
}
});
<script type="text/x-handlebars" data-template-name="exampleTemplate">
{{model.fullName}} - {{controller.fullName}}
</script>
模板:
Example = Ember.Application.create();
Example.User = Ember.Object.extend({
firstName: null,
lastName: null,
network: null,
fullName: function() {
return this.get('firstName') + " " + this.get('lastName');
}.property('firstName', 'lastName')
});
Example.IndexRoute = Ember.Route.extend({
model: function() {
var user = Example.User.create({
firstName: "John",
lastName: "Doe"
});
return user;
},
renderTemplate: function() {
this.render("exampleTemplate");
}
});
<script type="text/x-handlebars" data-template-name="exampleTemplate">
{{model.fullName}} - {{controller.fullName}}
</script>
{{model.fullName}-{{controller.fullName}
输出:
Example = Ember.Application.create();
Example.User = Ember.Object.extend({
firstName: null,
lastName: null,
network: null,
fullName: function() {
return this.get('firstName') + " " + this.get('lastName');
}.property('firstName', 'lastName')
});
Example.IndexRoute = Ember.Route.extend({
model: function() {
var user = Example.User.create({
firstName: "John",
lastName: "Doe"
});
return user;
},
renderTemplate: function() {
this.render("exampleTemplate");
}
});
<script type="text/x-handlebars" data-template-name="exampleTemplate">
{{model.fullName}} - {{controller.fullName}}
</script>
John Doe-John Doe但是如果我想为一个聊天应用程序创建模型用户的多个实例呢?然后从
模型
钩子返回一个实例数组,然后使用模板中的{{{each}}
Handlebar助手对它们进行迭代。