Ember routes deserialization not working on load - ember.js

I'm trying to get the Ember.js routing examples working.
I have a fiddle running pretty well with a posts example.
The routes update with the id of the post and display the correct item.
The only problem is if you load the page cold with the post url, it doesn't work (doesn't show anything in the posts view).
Any idea?
Here's the fiddle: http://jsfiddle.net/bdennyw/GzYtc/5/
and the fiddle with the full url
http://jsfiddle.net/bdennyw/GzYtc/5/show/#/posts/1
Thanks
Edit: Including the js code below:
$(function () {
App.initialize(App.Router.create());
});
window.App = Ember.Application.create();
App.Post = Ember.Object.extend({
title: null,
body: null
});
App.posts = [];
App.posts.pushObject(App.Post.create({id:'0', title: "Test post 1", body: "How awesome is Ember.js"}));
App.posts.pushObject(App.Post.create({id:'1', title: "Test post 2", body: "I love working on awesome projects"}));
App.posts.pushObject(App.Post.create({id:'2', title: "Test post 3", body: "I like cats"}));
App.ApplicationController = Ember.ObjectController.extend();
App.ApplicationView = Ember.View.extend({
templateName: "application_view"
});
App.PostsController = Ember.ArrayController.extend();
App.PostsView = Ember.View.extend({
templateName: 'posts_view'
});
App.PostController = Ember.ObjectController.extend();
App.PostView = Ember.View.extend({
templateName: 'post_view'
});
App.AboutController = Ember.ObjectController.extend();
App.AboutView = Ember.View.extend({
templateName: 'about_view'
});
App.Router = Ember.Router.extend({
root: Ember.Route.extend({
goToPostsIndex: Ember.State.transitionTo('posts.index'),
goToAbout: Ember.State.transitionTo('about'),
goToShowPost: Ember.State.transitionTo('posts.show'),
index: Ember.Route.extend({
route: '/',
redirectsTo: "posts.index"
}),
posts: Ember.Route.extend({
route: '/posts',
index: Ember.Route.extend({
route: '/',
connectOutlets: function (router) {
router.get('applicationController').connectOutlet('posts', App.posts);
}
}),
show: Ember.Route.extend({
route: '/:post_id',
connectOutlets: function (router, post) {
router.get('postsController').connectOutlet('post', post);
},
deserialize: function(router, params) {
var id = params.post_id,
i = 0;
for (i = 0; i < App.posts.length; i += 1) {
if (App.posts[i].id === id) {
return App.posts[i];
}
}
},
serialize: function(router, context) {
var rtnVal = {},
id = context.get('id');
if (context) {
rtnVal = {post_id: id};
}
return rtnVal;
},
}),
}),
about: Ember.Route.extend({
route: '/about',
connectOutlets: function (router) {
router.get('applicationController').connectOutlet('about');
}
})
})
});
​

By adding the enableLogging: true property to the router, you can see the routes taken by the router. So when loading the page, here is the trace:
STATEMANAGER: Entering root
Sending event 'navigateAway' to state root.
Sending event 'unroutePath' to state root.
Sending event 'routePath' to state root.
Entering root.posts
Sending event 'routePath' to state root.posts.
Entering root.posts.show
Sending event 'routePath' to state root.posts.show.
So It does'nt go through post.index which connect the outlet of your posts. So the view responsible of diplaying posts is not shown, and finally, the view responsible of displaying a post is not shown.
You could nest the show state as a child of the posts.index state, and it works, but I don't know if it's conceptually right.

Related

Implementing model filter

I have set up the following scaffolding for my Ember application.
window.App = Ember.Application.create({});
App.Router.map(function () {
this.resource('coaches', function() {
this.resource('coach', {path: "/:person_id"});
});
});
App.ApplicationAdapter = DS.FixtureAdapter.extend({});
App.Person = DS.Model.extend({
fname: DS.attr('string')
,lname: DS.attr('string')
,sport: DS.attr('string')
,bio: DS.attr('string')
,coach: DS.attr('boolean')
,athlete: DS.attr('boolean')
});
App.Person.FIXTURES = [
{
id: 10
,fname: 'Jonny'
,lname: 'Batman'
,sport: 'Couch Luge'
,bio: 'Blah, blah, blah'
,coach: true
,athlete: true
}
,{
id: 11
,fname: 'Jimmy'
,lname: 'Falcon'
,sport: 'Cycling'
,bio: 'Yada, yada, yada'
,coach: false
,athlete: true
}
];
I am trying to set up a route to filter the person model and return only coaches. Just to make sure I can access the data, I have simply used a findAll on the person model.
App.CoachesRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('person');
}
});
Now though, I am trying to implement the filter method detailed on the bottom of the Ember.js Models - FAQ page.
App.CoachesRoute = Ember.Route.extend({
model: function() {
var store = this.store;
return store.filter('coaches', { coach: true }, function(coaches) {
return coaches.get('isCoach');
});
}
});
The coaches route is not working at all with the new route implemented and the old one commented out. I am using the Ember Chrome extension and when using the filter route the console responds with, Error while loading route: Error: No model was found for 'coaches'. Apparently the route is not working, specifically the model. No kidding, right? What am I missing in my filter model route?
Thank you in advance for your help.
The error message is spot on- there is no CoachModel. I think you need to do this:
App.CoachesRoute = Ember.Route.extend({
model: function() {
var store = this.store;
return store.filter('person', { coach: true }, function(coaches) {
return coaches.get('isCoach');
});
}
});

Ember.js - Duplicate record in Store that clears after refresh

I have a basic Rest API and when I visit my newPost page, there is a form I can fill in with 'title' and 'text' fields as well as a submit button. After I create the record, I save it with the controller and transitionToRoute('posts').
When I view the 'posts' page, the input I just entered is displayed twice. I checked the Ember inspector and it looks like the record is put into the data store twice, once as I click the create button and one after the page refreshes. When I refresh the browser again, the duplicate is removed.
How do I get rid of the duplicate record in the store? Cheers for any suggestions.
EDIT: I believe I have solved my issue by using a filter to remove posts that hadn't been persisted yet.
Updated Routes:
App.PostsRoute = Ember.Route.extend({
model: function() {
return this.store.find('post');
},
setupController: function() {
var posts = this.store.filter('post', function (post) {
return !post.get('isDirty');
});
this.controllerFor('posts').set('model', posts);
}
});
App.NewPostRoute = Ember.Route.extend({
model: function(){
return this.store.createRecord('post');
}
});
Routes:
/* Routes */
App.Router.map(function() {
this.resource('posts');
this.resource('newPost');
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return this.store.find('post');
},
setupController: function() {
var posts = this.store.filter('post', function (post) {
return !post.get('isDirty');
});
this.controllerFor('posts').set('model', posts);
}
});
App.NewPostRoute = Ember.Route.extend({
model: function(){
return this.store.createRecord('post');
}
});
NewPostController:
actions:
createPost: function() {
...
var post = this.get('store').createRecord('post', {
title: title,
text: text
});
this.set('title', '');
this.set('text', '');
post.save();
this.transitionToRoute('posts');
...
newRecord: function() {
this.set('content', App.Post);
}
Thanks!

Redirect from index to another route and force model()

I'd like to forward the user to /articles when he arrives at /. Unfortunately, /articles's model()-function will not be executed because it's not a page refresh:
App.IndexRoute = Em.Route.extend
redirect: ->
#transitionTo "articles"
What's the Ember-way to achieve this?
I can't really tell the rest of your setup, but this is how I achieve it
window.App = Ember.Application.create();
App.Store = DS.Store.extend({
adapter: DS.FixtureAdapter
});
App.Router.map(function() {
this.resource('articles', function() {
this.resource('article', {path: ':article_id'});
});
});
App.Article = DS.Model.extend({
title: DS.attr('string')
});
App.Article.FIXTURES = [{
id: 1,
title: 'blah'
}, {
id: 2,
title: 'more blah'
}];
App.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('articles');
}
});
App.ArticlesRoute = Ember.Route.extend({
model: function() {
return App.Article.find();
}
});
working example:
http://jsbin.com/imidiq/2/

why doesn't the ember route update the url correctly when using dynamic segments?

this is a basic ember routing example that doesn't update the url to /posts correct when there is a child route in posts with dynamic segments. all the other routes (including the dynamic segments) correctly update the url. if i take out the dynamic segment child route (called 'show', under 'posts') then it updates the url correctly. here's the fiddle code : http://jsfiddle.net/inconduit/NbPpM/3/
and to view fiddle in action where it updates the urls, look here: http://fiddle.jshell.net/inconduit/NbPpM/3/show/#
to summarize - when you click 'Posts' the url should update to show /posts , but it does not.
here's the javascript:
App = Ember.Application.create({
ready: function() {
App.initialize(App.Router.create({ enableLogging: true }));
}
});
App.Post = Ember.Object.extend({
title: null,
body: null
});
App.posts = [];
App.posts.pushObject(App.Post.create({id:'0', title: "Test post 1", body: "How awesome is Ember.js"}));
App.posts.pushObject(App.Post.create({id:'1', title: "Test post 2", body: "I love working on awesome projects"}));
App.posts.pushObject(App.Post.create({id:'2', title: "Test post 3", body: "I like cats"}));
App.ApplicationController = Ember.ObjectController.extend();
App.ApplicationView = Ember.View.extend({
templateName: "application_view"
});
App.PostsController = Ember.ArrayController.extend();
App.PostsView = Ember.View.extend({
templateName: 'posts_view'
});
App.PostController = Ember.ObjectController.extend();
App.PostView = Ember.View.extend({
templateName: 'post_view'
});
App.AboutController = Ember.ObjectController.extend();
App.AboutView = Ember.View.extend({
templateName: 'about_view'
});
App.Router = Ember.Router.extend({
root: Ember.Route.extend({
goToPostsIndex: Ember.Route.transitionTo('posts.index'),
goToAbout: Ember.Route.transitionTo('about'),
goToShowPost: Ember.Route.transitionTo('posts.index.show'),
index: Ember.Route.extend({
route: '/',
redirectsTo: "posts.index"
}),
posts: Ember.Route.extend({
route: '/posts',
connectOutlets: function (router) {
router.get('applicationController').connectOutlet('posts', App.posts);
},
index: Ember.Route.extend({
route: '/',
connectOutlets: function (router) {
router.get('applicationController').connectOutlet('posts', App.posts);
},
show: Ember.Route.extend({
route: '/:post_id',
connectOutlets: function (router, post) {
router.get('postsController').connectOutlet('post', post);
},
deserialize: function(router, params) {
var id = params.post_id,
i = 0;
for (i = 0; i < App.posts.length; i += 1) {
if (App.posts[i].id === id) {
return App.posts[i];
}
}
},
serialize: function(router, context) {
var rtnVal = {},
id = context.get('id');
if (context) {
rtnVal = {post_id: id};
}
return rtnVal;
}
})
}),
}),
about: Ember.Route.extend({
route: '/about',
connectOutlets: function (router) {
router.get('applicationController').connectOutlet('about');
}
})
})
});
​`
Only leaf routes are meant to be navigable (i.e. the reason posts.index wasn't navigable is that it had a child route).
http://jsfiddle.net/NbPpM/8/
I moved posts.index.show to posts.show -- the way it is here is a common pattern, at least in my experience.

How do you test your emberjs routes?

After a few months without looking at emberjs, I'm trying to go back into it now, and I'm therefore trying the new router. And I would like to test my routes.
Has anybody tried to write some routing tests with emberjs ?
Let's suppose the very basic following router :
App.Router = Ember.Router.extend({
root: Ember.Route.extend({
index: Ember.Route.extend({
route: '/',
connectOutlets: function(router, context) {
router.get('applicationController').connectOutlet({name: 'home'});
}
})
})
})
How do you test that loading the root.index route properly loads the HomeView ?
Here is the full test, using Jasmine & Sinon :
code:
describe("Given the Router", function(){
var router = null;
beforeEach(function(){
router = Router.create();
});
afterEach(function(){
router = null;
});
it("Should be defined", function(){
expect(router).toBeDefined();
});
it("Should have an root route", function(){
expect(router.get("root")).toBeDefined();
});
describe("its root route", function(){
var root = null;
beforeEach(function(){
root = router.get("root").create();
});
afterEach(function(){
root = null;
});
it("should have an index route", function(){
expect(root.get("index")).toBeDefined();
});
describe("its index route", function(){
var indexRoute = null;
beforeEach(function(){
indexRoute = root.get("index").create();
});
it ("should have route of /", function(){
expect(indexRoute.get("route")).toEqual("/");
});
it ("should connect the outlets to home", function(){
var fakeRouter = Em.Object.create({applicationController: {connectOutlet: function(){} } });
var connectOutletSpy = sinon.spy(fakeRouter.applicationController, "connectOutlet");
var methodCall = connectOutletSpy.withArgs({name:"home"});
indexRoute.connectOutlets(fakeRouter);
expect(methodCall.calledOnce).toBeTruthy();
});
});
});
});
Hope it helps.
Here is how Ember has already tested the connectOutlet for you:
https://github.com/emberjs/ember.js/blob/master/packages/ember-views/tests/system/controller_test.js
test("connectOutlet instantiates a view, controller, and connects them", function() {
var postController = Ember.Controller.create();
var appController = TestApp.ApplicationController.create({
controllers: { postController: postController },
namespace: { PostView: TestApp.PostView }
});
var view = appController.connectOutlet('post');
ok(view instanceof TestApp.PostView, "the view is an instance of PostView");
equal(view.get('controller'), postController, "the controller is looked up on the parent's controllers hash");
equal(appController.get('view'), view, "the app controller's view is set");
});
Other routing related tests can be found at https://github.com/emberjs/ember.js/tree/master/packages/ember-routing/tests