将模型的属性呈现为 HTML 页面标题
Render an attribute of a model as the HTML page title
如何将User
模型的fullName
呈现为 HTML 页面标题而不是 "Details for ..."
中的...
?
App.User = DS.Model.extend({
firstName : DS.attr('string'),
lastName : DS.attr('string'),
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName'),
});
App.UserRoute = Em.Route.extend({
model: function(params){
return this.store.find('user', params.user_id);
},
activate: function() {
$(document).attr('title', 'Details for ...');
}
});
您可以在UserController
中观察 fullName
属性,并在属性更改时更新标题:
App.UserController = Ember.ObjectController.extend({
updateTitle: function() {
$(document).attr('title', 'Details for ' + this.get('fullName'));
}.observes('fullName')
})
若要仅设置一次标题而不进行绑定,可以使用以下内容:
App.UserRoute = Em.Route.extend({
originalTitle: null,
model: function(params){
return this.store.find('user', params.user_id);
},
activate: function() {
// save the original title
this.set('originalTitle', $(document).attr('title'));
// we use Ember.run.next because the currentModel property isn't avaliable
Ember.run.next(this, function() {
// the resolved result from model method, is set in the currentModel property
$(document).attr('title', 'Details for ' + this.currentModel.get('fullName'));
});
},
deactivate: function() {
// restore the original title
$(document).attr('title', this.get('originalTitle'));
}
});
这是 jsbin http://emberjs.jsbin.com/ExAkulA/3/edit
更新
我认为使用 afterModel
而不是activate
方法是实现它的更好方法:
App.UserRoute = Em.Route.extend({
originalTitle: null,
model: function(params){
return this.store.find('user', params.user_id);
},
afterModel: function(model) {
// save the original title
this.set('originalTitle', $(document).attr('title'));
// no hacks here, we have the resolved model avaliable
$(document).attr('title', 'Details for ' + model.get('fullName'));
},
deactivate: function() {
// restore the original title
$(document).attr('title', this.get('originalTitle'));
}
});
现场演示 http://emberjs.jsbin.com/ExAkulA/5/edit
相关文章:
- 如何强制即时显示更改后的html标题
- 如何使用JavaScript在HTML标题中正确插入unicode
- 我该如何为video.js制作一个插件,在HTML标题中添加和删除三角形
- HTML “标题”属性显示单词“未定义” - 例如.“主页未定义”
- “index.swf”嵌入在“index.html”中,在一个页面中显示同一网站两次.HTML 标题标签/属性也显示值“
- 使用铁路由器时设置 HTML 标题
- 在几秒钟后更改 HTML 标题
- 动态更新 url/html 标题时,如何获得类似 facebook 的按钮来注册更改?(不是 fb:like 中的 hr
- 可以在 HTML 标题标签中设置点击功能
- Javascript onclick 函数在 HTML 标题上不起作用
- 如何使用 javascript 数组填充 html 标题
- 将 HTML 标题转换为 JavaScript 变量
- 在点击函数中将 HTML 标题属性的值作为变量进行访问
- 从HTML标题和段落创建下拉菜单
- 如何根据其文件名自动更改HTML标题
- 如何使HTML标题属性停留更长时间
- 冻结宽度为100%的html标题
- 如何添加HTML标题属性到对象(flash)
- 浏览器在表标记中出现HTML标题标记问题
- 您可以在动态设置后使浏览器显示HTML标题属性吗?