2013-04-10 125 views
1

我有一個起始頁面,要求用戶輸入apiKey。使用該表單數據,我將其傳遞給我的deals路由,然後根據輸入的apiKey獲取相關數據。將對象模型作爲單個對象傳遞給ArrayController

我的問題是,當我直接與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> 
+0

你從控制檯發佈相同的錯誤信息?我看不到「asdf」來自 – MilkyWayJoe 2013-04-10 01:14:18

+0

是的。 'start'視圖包含一個TextField,我使用'apiKey'鍵入「asdf」並使用它下面的按鈕進行提交。 – brandonhilkert 2013-04-10 01:48:04

回答

1

這裏的問題是,當您向Route#transitionTo傳遞第二個參數時,Ember.js會假定您傳遞模型並將其設置爲控制器的模型,而不是使用model鉤子。

的問題是在這裏:

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

現在Ember.js認爲,this.apiKey是你的路由模式,將跳過呼叫路由的model鉤直接設置你的控制器的模型通過。

我能想到的解決此2種方式:

方法1(首選)

您可以創建一個新的資源用來包裝deals資源:

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

然後:

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

和:

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

方法2

下面這是一個快速的解決方法(可能不是最優的),但應該工作:

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); 
} 
+0

這太棒了。我實施了「方法1」,效果很好。我試圖圍繞爲什麼它的工作。因此,由於我們爲交易創建了單獨的資源,因此可以按預期的方式將Array設置爲Array,並將API資源保留爲普通對象資源? – brandonhilkert 2013-04-10 10:50:51

+0

是的,我們現在有2個資源:'api' - >'api_key'和'posts' - >帖子數組,因爲它嵌套在'api'資源中,所以'posts'資源不能存在。 – 2013-04-10 11:00:17

+0

感謝您的幫助!有太多的約定(這很好),但我仍然試圖將自己的頭腦和最佳實踐自動創建。 – brandonhilkert 2013-04-10 11:01:30

相關問題