this.transitionToRoute 在我的控制器中不工作 Ember
this.transitionToRoute not working in my controller Ember
我正在使用控制器读取在下拉菜单中选择的值,获取一些输入字段的参数,然后保存记录。它创建记录并很好地接收信息。我的问题在于当我尝试在操作结束时转换到另一个页面时。我不断收到错误消息:Cannot read property 'transitionToRoute' of undefined
我完全被难住了。有什么想法吗?
这是我的控制器代码:
var teamId;
export default Ember.Controller.extend({
auth: Ember.inject.service(),
actions: {
onSelectEntityType: function(value) {
console.log(value);
teamId = value;
return value;
},
createProcess: function(processName, processDescription) {
var currentID = this.get('auth').getCurrentUser();
let team = this.get('store').peekRecord('team', teamId);
let user = this.get('store').peekRecord('user', currentID);
let process = this.get('store').createRecord('process', {
team: team,
user: user,
name: processName,
description: processDescription
});
process.save().then(function () {
this.transitionToRoute('teams', teamId);
});
}
}
});
对应路线如下:
export default Ember.Route.extend({
auth: Ember.inject.service(),
model: function() {
var currentID = this.get('auth').getCurrentUser();
return this.store.find('user', currentID);
}
});
你应该对Javascript中的this关键字有清楚的认识。关键字 this 仅取决于函数的调用方式,而不是 how/when/where 它的定义方式。
function foo() {
console.log(this);
}
// normal function call
foo(); // `this` will refer to `window`
// as object method
var obj = {bar: foo};
obj.bar(); // `this` will refer to `obj`
// as constructor function
new foo(); // `this` will refer to an object that inherits from `foo.prototype`
查看 MDN documentation 以了解更多信息。
您可以将 this 缓存在普通变量 this 中,然后在回调中访问。
var self = this;
process.save().then(function () {
self.transitionToRoute('teams', teamId);
});
ECMASCript 6 引入了箭头函数,其 this 是词法范围的。在这里,就像普通变量一样在范围内查找。
process.save().then(() => {
this.transitionToRoute('teams', teamId);
});
我正在使用控制器读取在下拉菜单中选择的值,获取一些输入字段的参数,然后保存记录。它创建记录并很好地接收信息。我的问题在于当我尝试在操作结束时转换到另一个页面时。我不断收到错误消息:Cannot read property 'transitionToRoute' of undefined
我完全被难住了。有什么想法吗?
这是我的控制器代码:
var teamId;
export default Ember.Controller.extend({
auth: Ember.inject.service(),
actions: {
onSelectEntityType: function(value) {
console.log(value);
teamId = value;
return value;
},
createProcess: function(processName, processDescription) {
var currentID = this.get('auth').getCurrentUser();
let team = this.get('store').peekRecord('team', teamId);
let user = this.get('store').peekRecord('user', currentID);
let process = this.get('store').createRecord('process', {
team: team,
user: user,
name: processName,
description: processDescription
});
process.save().then(function () {
this.transitionToRoute('teams', teamId);
});
}
}
});
对应路线如下:
export default Ember.Route.extend({
auth: Ember.inject.service(),
model: function() {
var currentID = this.get('auth').getCurrentUser();
return this.store.find('user', currentID);
}
});
你应该对Javascript中的this关键字有清楚的认识。关键字 this 仅取决于函数的调用方式,而不是 how/when/where 它的定义方式。
function foo() {
console.log(this);
}
// normal function call
foo(); // `this` will refer to `window`
// as object method
var obj = {bar: foo};
obj.bar(); // `this` will refer to `obj`
// as constructor function
new foo(); // `this` will refer to an object that inherits from `foo.prototype`
查看 MDN documentation 以了解更多信息。
您可以将 this 缓存在普通变量 this 中,然后在回调中访问。
var self = this;
process.save().then(function () {
self.transitionToRoute('teams', teamId);
});
ECMASCript 6 引入了箭头函数,其 this 是词法范围的。在这里,就像普通变量一样在范围内查找。
process.save().then(() => {
this.transitionToRoute('teams', teamId);
});