2013-06-23 34 views
1

我正在努力处理动态的细分市场。这里是我的代码没有emberdata的动态细分市场

 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(); 

我应该把什么在StaffRoute,我停止获取No route matched the URL 'crisis'错误的模型?对于localhost/#stuff以及如何正确设置动态段部分?我唯一的问题是,所有的例子都使用了不支持生产的ember-data,我不想使用它。

回答

0

'/stuff/:stuff_id'只匹配/stuff/something而不是'/stuff'

尝试定义单独的资源:

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

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

,并使用App.StuffsRouteApp.StuffsView和此资源。

1

如果没有余烬数据,您通常会在路由上的model方法中直接将getJSON与jQuery放在一起。 model方法支持承诺,所以你可以重用jQuery的承诺。

例如给定加载的图像的列表使用Flickr的API将是/images/tag路线的路线,

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'); }); 
    } 
}); 

相应的控制器可以访问/绑定到该返回的JSON的自动特性。或者你可以别名一些计算属性。

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

然后使用这些属性通过句柄渲染它。

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

这是一个jsbin example这样做。