I have some kind of a "dash view" with a subset of items displayed. The item may "toggled" (by some navigation) for display in the main outlet. So far I lived without Ember's Router / StateManager stuff but instead I serialized/deserialized the URL manually and used window.history to enable back/foward and "restore by URL" features. e.g.
#/dash/foo,bar
#/dash/foo,bar,ted
should display the foo, bar and ted elements respectively. The items may be lazy loaded in the background. So if you restore the app state by bookmarking/directly editing the URL, the items actually may arrive some time later.
I try to employ Ember.Router for this stuff, but I just dont get it working...
I read through this great guide http://emberjs.com/guides/router_primer/. But the scenario described is always like: you have a collection view and a single item view. But that does not apply for my app.
In this scenario I have 3 items and I want to select some of them for display in the outlet, by toggling them. (In general this could be selecting,unselecting,toggling or search-by-expression). The selected-items-in-dash-state should be serialized to URL and be restorable by bookmarking or directly modyfing the URL.
Toggling is implemented like this:
<a {{action toggle name href=true}}>{{name}}</a>
This is how the data fetching and selection logic is right now (ITS NOT WORKING THOUGH)
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;
}
});
And that is the router:
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'
}
})
})
});
This is the complete fiddle http://jsfiddle.net/NWfbj/1
Any help is very appreciated!
itemsfonction, you callitems.clear(), then return items. is it what you really want ? - sly7_7