1

我有一个需要用户输入apiKey. 使用该表单数据,我将其传递给我的deals路线,然后根据输入的apiKey.

我的问题是,当我在 URI 中直接加载交易页面时apiKey,它可以正常工作,但是,当从start页面上的表单进入并使用 提交时apiKey,我收到以下错误:

Uncaught Error: assertion failed: an Ember.CollectionView's content must implement Ember.Array. You passed 'asdf'

这是 app.js:

App = Ember.Application.create();

App.Store = DS.Store.extend({
  revision: 12,
  adapter: 'DS.FixtureAdapter'
});

App.Deal = DS.Model.extend({
  name: DS.attr('string')
});

App.Deal.FIXTURES = [
  {id: 1, name: 'Deal 1'},
  {id: 2, name: 'Deal 2'}
]

App.Router.map(function() {
  this.resource('start', { path: '/' });
  this.resource('deals', { path: '/deals/:api_key' });
});

App.StartController = Ember.ObjectController.extend({
  apiKey: '',
  getDeals: function (model) {
    this.transitionToRoute('deals', this.apiKey);
  }
});

App.DealsRoute = Ember.Route.extend({
  model: function() {
    // return App.Deal.getWonFor(params.api_key);
    return App.Deal.find();
  }
});

App.DealController = Ember.ArrayController.extend({
});

App.DealsView = Ember.View.extend({
  didInsertElement: function() {
    // Add active class to first item
    this.$().find('.item').first().addClass('active');
    this.$().find('.carousel').carousel({interval: 1000});
  }
});

这是HTML:

  <script type="text/x-handlebars">
    <h2>Won Deal Ticker</h2>
    {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="start">
    {{view Em.TextField valueBinding="apiKey" placeholder="API Key" action="getDeals" }}
    <br />
    <button {{action 'getDeals' apiKey}} class="btn btn-large">Get Won Deals!</button>
  </script>

  <script type="text/x-handlebars" data-template-name="deals">
    <div id="carousel" class="carousel slide">
      <div class="carousel-inner">
        {{#each model}}
          <div class="item">
            {{name}}
          </div>
        {{/each}}
      </div>
    </div>
  </script>
4

1 回答 1

1

The issue here is that when you pass a second argument to Route#transitionTo, Ember.js assumes you're passing the model and sets it as the controller's model instead of using the model hook.

The problem is here:

this.transitionToRoute('deals', this.apiKey);

Now Ember.js believes that this.apiKey is the model for your route, and will skip calling the route's model hook and directly set what you passed as the controller's model.

I can think of 2 ways around this:

Method 1 (preferred)

You can create a new resource that wraps the deals resource:

this.resource('api', { path: '/:api_key' }, function() {
  this.resource('deals');
});

And then:

App.ApiRoute = Ember.Route.extend({
  model: function(params) {
    return params.api_key;
  }
});

App.DealsRoute = Ember.Route.extend({
  model: function() {
    return App.Deal.getWonFor(this.modelFor('api'));
  }
});

And:

getDeals: function () {
  this.transitionToRoute('deals', this.apiKey);
}

Method 2

This below is a quick workaround (probably not optimal), but should work:

getDeals: function () {
  var router = this.get('target'),
      url = '/deals/' + this.apiKey;

  Ember.run.once(function() {
    router.get('location').setURL(url);
    router.notifyPropertyChange('url');
  });

  this.router.router.handleURL(url);
}
于 2013-04-10T07:53:03.183 回答