【问题标题】:Ember dynamic segments without emberdata没有 emberdata 的 Ember 动态段
【发布时间】:2013-06-23 16:31:23
【问题描述】:

我正在为路线的动态路段而苦苦挣扎。这是我的代码

        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 文档的唯一问题是,所有示例都使用未准备好生产的 ember-data,我不想使用它。

【问题讨论】:

    标签: javascript ember.js ember-data


    【解决方案1】:

    如果没有 ember-data,您通常会将带有 jQ​​uery 的直接 getJSON 放在路由上的 model 方法中。 model 方法支持 Promise,因此您可以重用 jQuery Promise。

    例如,给定使用 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 可以做到这一点。

    【讨论】:

      【解决方案2】:

      '/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.StuffsViewApp.StuffsController

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-05
        • 1970-01-01
        • 1970-01-01
        • 2014-08-16
        • 1970-01-01
        • 2014-04-17
        相关资源
        最近更新 更多