I am just starting with emberjs. I am creating a simple index.html page with two links on top: About and Posts. This is following the standard example on emberjs homepage. I get an error in the browser
"Uncaught TypeError: Cannot call method 'map' of undefined"
So this means that my router is undefined. Why is that?
Here is the app.js code:
var App = Ember.Application.create();
App.router.map(function(){
});
So I tried to define it ...
var App = Ember.Application.create();
App.Router = Ember.Router.extend({
enableLogging: true,
location: 'hash'
});
App.Router.map(function(){
});
I still get the error. I am confused :(.
Make sure you are using the latest version of Ember (1.0.0-RC.2) from http://emberjs.com/.
The example you posted is the old style router which is not used anymore. The new style is explained in this guide and is defined like this:
App.Router.map(function() {
this.route("about", { path: "/about" });
this.route("favorites", { path: "/favs" });
});
A resource is not an end point. Try the following:
App.Router.map(function() {
this.resource('about', function() {
});
});
So I came across another question on stackoverflow. http://goo.gl/JVEs9. I got a version of ember (1.0.0-pre.2) from the link and re-wrote the code as follows:
var App = Ember.Application.create();
App.Router.map(function(match){
match('/').to('index');
match('about').to('about');
});
This seems to work. I still cannot get:
var App = Ember.Application.create();
App.Router.map(function(){
this.resource('about');
});
I get the error
Uncaught TypeError: Object # has no method 'resource'
Related
i try to build an webapp with emberjs. this is the app router:
App.Router.map(function() {
this.route("page");
this.route("menu");
this.resource('posts', function(){
this.resource('post', { path: '/:post_id' });
});
this.route("index", { path: "/" });
this.route("cat");
this.route("foto");
});
and this is the Postroute:
// GET POST JSON
App.PostRoute = Ember.Route.extend({
model: function(params) {
return Ember.$.getJSON('http://www.wilhelminaschool.eu/?json=get_post&post_id='+params.post_id);
}
});
but i get an route not found error for the post, the posts list works. What i am doing wrong?
error:
Error: Assertion Failed: The route post/11330 was not found
live on:
http://www.wilhelminaschool.eu/app2/#posts
http://www.wilhelminaschool.eu/app2/#post/11330
Each one of the posts links to app2/#post/:post_id i.e. app2/#post/11330 , but since the post resource has been defined within the resource of posts with a path of /:post_id the links should be app2/#/posts/:post_id i.e. app2/#/posts/11330
example,
http://emberjs.jsbin.com/budeyaja/1/edit
http://emberjs.jsbin.com/budeyaja/1 (please observe the urls while navigating)
If you need the links to work as they are then the routes will have to be specified as ,
this.resource("posts");
this.resource("post", {path:"/post/:post_id"});
http://emberjs.jsbin.com/qaquzusi/1/edit
Do not nest the post resource inside posts, use:
this.resource('posts');
this.resource('post', { path: '/post/:post_id' });
Ember Routing Guide provides a clear explanation of different cases.
I have been trying to set up an Ember.js application together with a RESTful API i have created in Laravel.
I have encountered a problem trying to get the data trough the store, and depending on my implementation, I get different errors, but never any working implementations.
The ember.js guide have one example, other places have other examples, and most information I find is outdated.
Here's my current code:
App = Ember.Application.create();
App.Router.map(function() {
this.resource("world", function() {
this.resource("planets");
});
});
App.PlanetsRoute = Ember.Route.extend({
model: function() {
return this.store.find('planet');
}
});
App.Planet = DS.Model.extend({
id: DS.attr('number'),
name: DS.attr('string'),
subjectId: DS.attr('number')
});
And when I try to click the link for planets, thats when the error occurs, and I get the following error right now:
Error while loading route: TypeError {} ember-1.0.0.js:394
Uncaught TypeError: Cannot set property 'store' of undefined emberdata.js:15
No request is sent for /planets at all. I had it working with a $.getJSON, but I wanted to try to implement the default ember-data RESTAdapter.
For reference, these are some of the implementations i've tried:
var store = this.get('store'); // or just this.get('store').find('planet')
return store.find('planet', 1) // (or findAl()) of store.findAll('planet');
App.store = DS.Store.create();
I also tried DS.Store.all('planet') as I found it in the ember.js api, but seemed like I ended up even further away from a solution.
Most other implementations give me an error telling me there is no such method find or findAll.
EDIT (Solution)
After alot of back and forward, I managed to make it work.
I'm not sure exactly which step fixed it, but I included the newest versions available from the web (Instead of locally), and the sourcecode now looks like this:
window.App = Ember.Application.create();
App.Router.map(function() {
this.resource("world", function() {
this.resource("planets");
});
});
App.PlanetsRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('planet');
}
});
App.Planet = DS.Model.extend({
name: DS.attr(),
subjectId: DS.attr()
});
The error you had is probably due to the fact that you added a "s" plural of your objects.
i.e. if you use
App.Planets = DS.Model.extend({
})
you would get that error.
I've just started using Ember.js and have got stuck at the first hurdle!
My URL structure is /username/action, so for example /matt/feed (or just /matt with a default view).
But, although I've managed to get a hardcoded username value to output something, I don't know how to have the username part of the URL be a variable which I can then do things with.
The code I'm using to hardcode the username is below, haven't really got anywhere because I'm not really sure how to get Ember.js to understand my URLs!
'use strict';
window.App = Ember.Application.create();
App.Router.reopen({
location:'history'
});
App.Router.map(function() {
this.resource('matt');
});
Right from the documentation at http://emberjs.com/guides/routing/defining-your-routes/#toc_dynamic-segments
App.Router.map(function() {
this.resource('posts');
this.resource('post', { path: '/post/:post_id' });
});
:post_id is the dynamic segment of the route.
I'm trying to start to build a admin system that will run on a /admin/ prefix.
Here is my routes file
App.Router.reopen
location: 'history'
rootURL: '/admin'
App.IndexRoute = Ember.Route.extend
setupController: (controller, model) ->
#controllerFor('application').set('currentRoute', 'home')
When I go to /admin I get the following error:
Uncaught Error: No route matched the URL '/admin'
I'm just starting with emberjs, and my code is based on this serie
Ember version: v1.0.0-pre.4
Ember-data current api revision:: 11
In old-router the 'rootURL' property would have been ignored when resolving routes. In the latest version of ember, rootURL only seems to be used when constructing links. Not sure if this is a bug or oversight. As a workaround, try this instead:
App.Router.map(function() {
this.resource("admin", { path: "/admin" }, function() {
this.route("other");
});
});
App.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('admin');
}
});
When talking about routing in emberjs, it depends which version you are using. There was a big API change between 1.0pre2 and 1.0pre3. The docu on www.emberjs.com is already up-to-date for the new API and and easy to understand.
Below a really small example that shows
IndexRoute that automatically redirects to the overview of all members at '/members'.
Dynamic routing based on an ID
Serialization/Deserialization in case that the parameter is not 'id' but something else. In the example below, it is 'refId' (stands for reference ID).
Well, the examle does not really show more than the official documentation. but add-on information is always nice.
So, hope this helps. cheers.
App.Router.map(function() {
this.resource("members", { path: '/members' });
this.resource("member", { path: "/members/:refId" }, function() {
this.route("delete");
});
});
App.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('members');
}
});
App.MembersRoute = Ember.Route.extend({
model: function() {
return App.Member.findAll();
}
});
App.MemberRoute = Ember.Route.extend({
model: function(params) {
return App.Member.find(params.refId);
},
// overwrite default serializer (defaults to 'id', member has 'refId')
serialize: function(model) {
return { refId: model.refId };
}
});
i'm getting error with ember 0.9.8.1
You cannot use the same root element (body) multiple times in an Ember.Application
any idea what this is happening? some suggestions on where i should look into?
thanks.
You cannot bind several Ember application to the same DOM element, as it will conflict for DOM maintenance.
You nevertheless can instanciate several Ember applications in the same page. Try something like that:
App1 = Ember.Application.create({
rootElement: '#app1'
});
App1.ApplicationController = Ember.Controller.extend();
App1.ApplicationView = Ember.View.extend({
templateName: 'app1-view'
})
App1.Router = Ember.Router.extend({
root: Ember.Route.extend({
index: Ember.Route.extend({
path: '/'
})
})
});
App2 = Ember.Application.create({
rootElement: '#app2'
});
App2.ApplicationController = Ember.Controller.extend();
App2.ApplicationView = Ember.View.extend({
templateName: 'app2-view'
})
App2.Router = Ember.Router.extend({
root: Ember.Route.extend({
index: Ember.Route.extend({
path: '/'
})
})
});
Here, we explicitly set the DOM element to which the app will bind, using rootElement property.
By default, an Ember app binds to body, so if you have twice, they conflict...
Example # http://jsfiddle.net/MikeAski/FMV8u/13/