[英]ember model function not executing when route is activated
我有一條余燼路線,
App.MyRoute = Ember.Route.extend({
model: function(params){
console.log("model function executing");
Ember.Object.create()
},
setupController: function(controller){
console.log("setupController function executed");
}
});
當我切換到MyRoute時,會執行setupController,但是填充模型的函數卻從未執行過。 該模型最終只是在{{link myRoute msg}}
標記中傳遞的msg
對象。
在切換到該路線時,我需要加載/計算模型的某些部分。 為此,我需要能夠成功更新模型,或者需要從setupController函數中訪問鏈接中傳遞的參數。 關於如何最好地實現這一目標的建議?
編輯
為了嘗試解決這個問題,我創建了一個完整的最小示例,它將產生以下行為:
我的html是:
<html>
<head>
<title> This is my Page! </title>
<script src="js/libs/jquery-1.8.2.js"></script>
<script src="js/libs/handlebars-1.0.rc.1.js"></script>
<script src="js/libs/ember.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<script type="text/x-handlebars">
{{#linkTo example App.thing}}<p> go </p>{{/linkTo}}
<div>
{{outlet}}
</div>
</script>
<script type="text/x-handlebars" data-template-name="index">
<p> Initial Text </p>
</script>
<script type="text/x-handlebars" data-template-name="example">
<p> After change </p>
</script>
</body>
</html>
使用應用代碼:
var App = Ember.Application.create();
App.Router.map(function() {
this.resource("example", {path: "/example/:id"});
});
App.thing = Ember.Object.create({
id:10,
});
App.ExampleRoute = Ember.Route.extend({
model: function(params){
console.log("in model function");
return new Ember.Object.create();
},
setupController: function(controller){
console.log("in setupController");
}
});
當您單擊示例路線的鏈接時,將打印“在setupController中”,但不會打印“在模型函數中”。
linkTo
在linkTo
使用transitionTo
。 每當我們使用transitionTo
,我們實際上都會直接提供上下文/模型,因此不會調用路由上的model
方法。 在上面的示例中,您有{{#linkTo example App.thing}}
。 由於我們已經知道上下文是App.thing
因此沒有理由觸發model
方法。 我們僅在不知道模型是什么時才在路線上調用model
。 發生這種情況的主要時間是通過URL更改輸入時。
它似乎對我有用,並且絕對沒有理由不起作用: http : //jsfiddle.net/SXTME/
您確定使用的是EMBER 1.0.0-PRE.4,而不是EMBER 1.0.0-PRE.3嗎?
App.MyRoute = Ember.Route.extend({
model: function(params){
console.log("model function executing");
Ember.Object.create()
},
setupController: function(controller){
console.log("setupController function executed");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.