模塊化灰燼應用工作intermittently-代碼這個帖子有很多細節,所以請多多包涵與requirejs
我的大部分Ember.js開發經驗是預發佈版(0.9.8.1)和我相當新的路線。我已經投入了大量的時間瞭解他們,創造了一個工作程序:
問題是,我的代碼間歇性不工作(在Chrome中大多顯着)。起初,我遇到了無法找到模板的問題。我重寫了路線圖,並指定像這樣的模板:
腳本/ users.js
define(['scripts/app','text!scripts/templates/UserIndex.html'],
function (app, templateUserIndex) {
//route is called app.UserIndexRoute
app.UserIndexView = Ember.View.extend({
template: Ember.Handlebars.compile(templateUserIndex),
Created: function() {
return app.ParseDate(this.get('controller.model.created')).toLocaleDateString();
}.property('controller.model.created'),
MailTo: function() {
return 'mailto:' + this.get('controller.model.email');
}.property('controller.model.email')
});
});
我驗證requirejs加載使用文本插件HTML ...但後來發現, StackOverflow上的其他人在路由對象中使用「模板」和視圖時遇到問題。因此,我沒有與之對抗,而是移除了「模板」行,並更新了我的App類,以明確設置ready()事件中所有視圖的模板(因爲我知道路由將默認模板名稱)
scripts/app.js
define(['jquery',
'handlebars',
'ember',
'text!scripts/templates/Application.html',
'text!scripts/templates/Loading.html',
'text!scripts/templates/Index.html',
'text!scripts/templates/OwnedCarsIndex.html',
'text!scripts/templates/OwnedCar.html',
'text!scripts/templates/UserIndex.html'],
function (jQuery, handlebars, ember, templateApplication, templateLoading, templateIndex, templateOwnedCarsIndex, templateOwnedCar, templateUserIndex) {
App = Ember.Application.create({
ready: function() {
Ember.TEMPLATES['application'] = Ember.Handlebars.compile(templateApplication);
Ember.TEMPLATES['loading'] = Ember.Handlebars.compile(templateLoading);
Ember.TEMPLATES['index'] = Ember.Handlebars.compile(templateIndex);
Ember.TEMPLATES['ownedCars/index'] = Ember.Handlebars.compile(templateOwnedCarsIndex);
Ember.TEMPLATES['ownedCar'] = Ember.Handlebars.compile(templateOwnedCar);
Ember.TEMPLATES['user/index'] = Ember.Handlebars.compile(templateUserIndex);
},
LOG_TRANSITIONS: true,
LOG_TRANSITIONS_INTERNAL: true,
LOG_VIEW_LOOKUPS: true,
LOG_ACTIVE_GENERATION: true
});
App.Router.map(function() {
this.resource('users', function() {
this.resource('user', { path: ':user_id' }, function() {
this.resource('ownedCars', { path: 'cars' }, function() {
this.resource('ownedCar', { path: ':ownership_id' }, function() {
this.resource('expenses');
});
});
});
});
this.resource('cars', function() {
this.resource('car', { path: ':car_id' });
});
});
return App;
});
這樣就解決了模板間歇無法加載的問題。但是現在又間歇性地,我遇到了這條路線的一個問題。我有一個鏈接來幫手,這裏是一些控制檯輸出爲網頁時遇到這個問題:
DEBUG: -------------------------------
DEBUG: Ember : 1.3.0
DEBUG: Handlebars : 1.1.2
DEBUG: jQuery : 1.10.2
DEBUG: -------------------------------
Attempting URL transition to/
generated -> route:application Object {fullName: "route:application"}
generated -> route:index Object {fullName: "route:index"}
Transition #1: Beginning validation for transition to index
Transition #1: application: calling beforeModel hook
Transition #1: application: resolving model
Transition #1: application: calling afterModel hook
Transition #1: application: validation succeeded, proceeding
Transition #1: index: calling beforeModel hook
Transition #1: index: resolving model
Transition #1: index: calling afterModel hook
Transition #1: index: validation succeeded, proceeding
Transition #1: Validation succeeded, finalizing transition;
generated -> controller:application Object {fullName: "controller:application"}
Rendering application with default view <(subclass of Ember.View):ember209> Object {fullName: "view:application"}
generated -> controller:index Object {fullName: "controller:index"}
Rendering index with default view <Ember._MetamorphView:ember225> Object {fullName: "view:index"}
Transitioned into 'index'
Transition #1: TRANSITION COMPLETE.
generated -> route:cars Object {fullName: "route:cars"}
generated -> route:cars.index Object {fullName: "route:cars.index"}
generated -> route:users Object {fullName: "route:users"}
generated -> route:user Object {fullName: "route:user"}
generated -> route:user.index Object {fullName: "route:user.index"}
,但是當我嘗試點擊「修改個人資料」鏈接,將其用在鏈路中產生要幫手,我得到這個:
Attempting transition to user.index
Transition #2: Beginning validation for transition to user.index
Transition #2: application: using context from already-active handler
Transition #2: application: validation succeeded, proceeding
Transition #2: users: calling beforeModel hook
Transition #2: users: resolving model
Transition #2: users: calling afterModel hook
Transition #2: users: validation succeeded, proceeding
Transition #2: user: calling beforeModel hook
Transition #2: user: resolving model
Assertion failed: You used the dynamic segment user_id in your route user, but App.User did not exist and you did not override your route's `model` hook.
Transition #2: user.index: transition was aborted
我不知道爲什麼它試圖訪問App.User,而不是App.UserRoute(App.UserRoute具有重載模式功能,正確生成的模型)
任何幫助非常感謝:)
更新:我有(我甚至沒有提及) 一個問題我solved-我需要使用requirejs的配置勻場Ember.js:
require.config({
paths: {
'text': 'scripts/references/text',
'jquery': 'scripts/references/jquery-1.10.2.min',
'handlebars': 'scripts/references/handlebars-1.1.2',
'ember': 'scripts/references/ember-1.3.0'
},
shim: {
'ember': {
deps: ['handlebars', 'jquery'],
exports: 'Ember'
}
}
});
這擺脫了間歇性「模塊未找到jquery「或」找不到模塊句柄「(這隻發生在轉儲緩存時發生)。所描述的主要問題仍然存在。從調試器控制檯,這裏是我注意到,當它呈現在網頁,唯一的區別...
工作頁
Transition #1: TRANSITION COMPLETE.
generated -> route:cars Object {fullName: "route:cars"}
generated -> route:users Object {fullName: "route:users"}
不工作頁面(錯誤當你點擊鏈接)
Transition #1: TRANSITION COMPLETE.
generated -> route:cars Object {fullName: "route:cars"}
generated -> route:cars.index Object {fullName: "route:cars.index"}
generated -> route:users Object {fullName: "route:users"}
generated -> route:user Object {fullName: "route:user"}
generated -> route:user.index Object {fullName: "route:user.index"}
如果您發現非工作頁面輸出,Ember正在爲尚未使用的路由生成路由對象...爲什麼有時候這隻會發生?
想要提出這個問題後的幾個月 - 我找到並切換到Ember CLI。我強烈建議使用它,而不是像上面那樣做任何事情。 –