Pio
Pio

Reputation: 4064

Ember dynamic segments without emberdata

I'm struggling with the dynamic segments of routes. Here is my code

        App.Router.map(function(){
        this.resource('stuff', {path: '/stuff/:stuff_id'}, function() {
          this.route('make');
          this.route('edit');
          this.route('delete');
          this.route('history');
          });
        });

        App.StuffRoute = Ember.Route.extend({
            model: function(param) {
            },
        setupController: function (){
            },
            renderTemplate: function() {
            }
        });

       App.StuffView= Ember.View.extend({
         defaultTemplate: Ember.Handlebars.compile(stuffTemplate)
       });

       App.StuffController = Ember.Controller.extend();

What should I put in the model of StaffRoute that I stop getting No route matched the URL 'crisis' errors? for localhost/#stuff and how can I set up properly the dynamic segment part? My only problem with the ember documentation is, that all the examples use ember-data that is not production ready and I don't want to use it.

Upvotes: 1

Views: 167

Answers (2)

ciastek
ciastek

Reputation: 1363

'/stuff/:stuff_id' matches only /stuff/something, not '/stuff'.

Try to define separate resource:

App.Router.map(function(){
this.resource('stuffs', {path: '/stuff'});
this.resource('stuff', {path: '/stuff/:stuff_id'}, function() {
    // routes ...
});

or

App.Router.map(function(){
this.resource('stuffs', {path: '/stuff'}, function() {
    this.resource('stuff', {path: '/:stuff_id'}, function() {
        // routes ...
    });
});

and use App.StuffsRoute, App.StuffsView and App.StuffsController for this resource.

Upvotes: 0

Darshan Sawardekar
Darshan Sawardekar

Reputation: 5075

Without ember-data you would typically put direct getJSON with jQuery inside the model method on the route. The model methods supports promises so you can reuse the jQuery promise.

For instance given the route to load a list of images for the /images/tag route using the Flickr api would be,

App.Router.map(function() {
  this.resource('images', { path: '/images/:tag'});
});

App.ImagesRoute = Ember.Route.extend({
  model: function(params) {
    flickerAPI = 'http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?';
    console.log('ImagesRoute.model', params);

    return jQuery.getJSON( flickerAPI, {
      tags: params.tag,
      tagmode: 'any',
      format: "json"
    })
    .then(function(data) {
      console.log('loaded images', data);
      return data;
    })
    .then(null, function() { console.log('failed to load images'); });
  }
});

The corresponding controller can access/bind to the properties of the returned json automatically. Or you can alias a few computed properties.

App.ImagesController = Ember.ObjectController.extend({
  images: function() {
    return this.get('model').items;
  }.property('controller'),
  title: function() {
    return this.get('model').title;
  }.property('images')
});

And then render it via handlebars using these properties.

<script type='text/x-handlebars' data-template-name='images'>
<h1>{{title}}</h1>
{{#each image in images}}
  <img {{bindAttr src='image.media.m'}} />
{{/each}}
</script>

Here's a jsbin example that does this.

Upvotes: 1

Related Questions