我有某种“破折号视图”,其中显示了一部分项目。 该项目可以“切换”(通过某种导航)以在主插座中显示。到目前为止,我没有使用Ember的Router / StateManager,但是我手动对URL进行了序列化/反序列化,并使用了window.history来启用back / foward和“通过URL恢复”功能。 例如
#/dash/foo,bar
#/dash/foo,bar,ted
应分别显示foo,bar和ted元素。这些项目可能在后台延迟加载。因此,如果您通过书签/直接编辑URL来恢复应用程序状态,那么这些项目实际上可能会在一段时间后到达。
我尝试使用Ember.Router来做这些事情,但我只是不让它工作......
我通读了这本伟大的指南http://emberjs.com/guides/router_primer/。 但描述的场景总是如下:您有一个集合视图和一个项目视图。但这不适用于我的应用程序。
在这种情况下,我有3个项目,我想通过切换它们来选择其中一些项目在插座中显示。 (通常,这可以是选择,取消选择,切换或按表达式搜索)。选中的项目状态应该序列化为URL,并通过书签或直接修改URL来恢复。
切换是这样实现的:
<a {{action toggle name href=true}}>{{name}}</a>
这就是数据提取和选择逻辑现在的方式(它没有工作)
App.dash = Ember.Object.create({
FIXTURE: [App.Item.create({
name: 'foo',
detail: 'Some very interesting foo'
}),App.Item.create({
name: 'bar',
detail: 'Bar is the root of all stuff'
}),App.Item.create({
name: 'ted',
detail: 'Ted is not my name'
})],
store: Ember.A(),
selected: Ember.A(),
_items: Ember.A(),
watchItems: function () {
var items = this._items;
var store = this.store;
items.clear();
this.get('selected').forEach(function (name) {
var item = store.findProperty('name', name);
items.pushObject(item);
});
}.observes('selected', 'store'),
toggle: function (name) {
var selected = this.get('selected');
var index = selected.indexOf(name);
if (index !== -1) {
selected.removeAt(index);
} else {
selected.push(name);
}
this.set('selected', selected);
},
fetch: function () {
var FIXTURE = this.FIXTURE;
var store = this.store;
if (store.length == 0) {
Ember.run.later(function () {
FIXTURE.forEach(function (item) {
store.pushObject(item);
});
}, 2000);
}
},
items: function (selected) {
var items = this._items;
items.clear();
if (Ember.isArray(selected)) {
this.set('selected', selected);
} else {
this.set('selected', []);
}
this.fetch();
return items;
}
});
那就是路由器:
App.Router = Ember.Router.extend({
root: Ember.Route.extend({
index: Ember.Route.extend({
route: '/',
redirectsTo: 'dash'
}),
dash: Ember.Route.extend({
route: '/dash/:selected',
connectOutlets: function (router, context) {
var selected = context && context.selected;
var dashItems = App.dash.items(selected);
router.get('applicationController').connectOutlet('dash', dashItems);
},
deserialize: function (router, params) {
if (params.selected !== 'undefined') {
return params.selected;
}
},
serialize: function (router, context) {
var selected = App.dash.get('selected');
var hash = {
selected: selected
};
return hash;
},
toggle: function (router, event) {
event.preventDefault();
event.stopPropagation();
var name = event.context;
App.dash.toggle(name);
var selected = App.dash.get('selected');
router.transitionTo('dash', {
selected: selected
});
},
eventTransitions: {
toggle: 'dash'
}
})
})
});
这是完整的小提琴http://jsfiddle.net/NWfbj/1
非常感谢任何帮助!
答案 0 :(得分:1)
好吧,我尝试修理一些事情:
访问Ember项目中的属性时,一个好的规则是始终使用get('property')/ set('property',value')
如果要观察数组内容,请使用特殊的“@each”。然后在添加/删除阵列中的任何对象时触发观察者。
我在items()函数中删除了我认为额外的items.clear()。
如果你想能够通过URL操作,我认为你必须检查deserialize方法,以便返回你所期望的(我不知道它是否应该是字符串数组,一个项目数组(从商店中检索出来,这取决于你:)
你可以看到我在这里做了什么:http://jsfiddle.net/Sly7/NWfbj/23/
<script type="text/x-handlebars" data-template-name='application'>
<h1>Dash Router App</h1>
{{#each App.dash.store}}
<a {{action toggle name href=true}}>{{name}}</a>
{{/each}}
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name='dash'>
{{#each controller}}
<h1>{{detail}}</h1>
{{/each}}
</script>
App = Ember.Application.create();
App.Item = Ember.Object.extend({
name: null,
detail: null
});
App.dash = Ember.Object.create({
FIXTURE: [{
name: 'foo',
detail: 'Some very interesting foo'
}, {
name: 'bar',
detail: 'Bar is the root of all stuff'
}, {
name: 'ted',
detail: 'Ted is not my name'
}],
store: [],
selected: [],
_items: [],
watchItems: function () {
console.log('watch items', this.get('selected'));
var self = this;
this.get('_items').clear();
this.get('selected').forEach(function (name) {
var item = self.get('store').findProperty('name', name);
console.log(name);
self.get('_items').pushObject(item);
});
}.observes('selected.@each', 'store.@each'),
toggle: function (name) {
var selected = this.get('selected');
var index = selected.indexOf(name);
if (index !== -1) {
selected.removeObject(name);
} else {
selected.pushObject(name);
}
console.log('toggle', name, 'is now selected?', selected.indexOf(name) !== -1, selected);
this.set('selected', selected);
},
fetch: function () {
var FIXTURE = this.get('FIXTURE');
var store = this.get('store');
if (store.length == 0) {
Ember.run.later(function () {
FIXTURE.forEach(function (item) {
store.pushObject(item);
});
}, 2000);
}
},
items: function (selected) {
if (Ember.isArray(selected)) {
this.set('selected', selected);
} else {
this.set('selected', []);
}
this.fetch();
return this.get('_items');
}
});
App.ApplicationController = Ember.Controller.extend({});
App.ApplicationView = Ember.View.extend({
templateName: 'application'
});
App.DashView = Ember.View.extend({
templateName: 'dash'
});
App.DashController = Ember.ArrayController.extend({});
App.Router = Ember.Router.extend({
enableLogging: true,
root: Ember.Route.extend({
index: Ember.Route.extend({
route: '/',
redirectsTo: 'dash'
}),
dash: Ember.Route.extend({
enter: function () {
console.log('entering dash');
},
exit: function () {
console.log('exiting dash');
},
route: '/dash/:selected',
connectOutlets: function (router, context) {
debugger;
var selected = context && context.selected;
console.log('connectOutlets', 'selected:', selected);
var dashItems = App.dash.items(selected);
console.log('connectOutlets', dashItems);
router.get('applicationController').connectOutlet('dash', dashItems);
},
deserialize: function (router, params) {
debugger;
console.log('deserialize', 'selected:', params.selected);
if (params.selected !== 'undefined') {
return params.selected;
}
},
serialize: function (router, context) {
var selected = App.dash.get('selected');
var hash = {
selected: selected
};
console.log('serialize', 'selected:', selected);
return hash;
},
toggle: function (router, event) {
debugger;
event.preventDefault();
event.stopPropagation();
var name = event.context;
console.log('clicked',name);
App.dash.toggle(name);
var selected = App.dash.get('selected');
console.log('transitionTo', 'selected:', selected);
router.transitionTo('dash', {
selected: selected
});
},
eventTransitions: {
toggle: 'dash'
}
})
})
});
App.initialize();