2013-05-12 114 views
4

由於以下問題,我現在被困在一起了幾天。我的用例是我有一個擁有數百萬個地址的數據庫。從網絡應用程序中,我想搜索它們,顯示退出列表,然後顯示有關單個地址的信息。一個重要目標是將搜索條件表示爲URL的一部分。這樣用戶可以回到以前的搜索,甚至通過操縱URL來構建搜索。所以,我想有一個URL是這樣的:帶有動態過濾器的搜索路線/搜索標準

http://localhost/#/searchresults?lastname=king&firstname=stephen&city=somecity 

我不管理設置路由器來處理這些類型的網址。所有的嘗試都以死衚衕結束。如何使ember進入/ searchresults路線,以及如何使用RESTAdapter讓它轉發這些過濾條件?

回答

5

基礎上的從直觀的像素轉向我想出了以下解決方案。

我構建了以下網址: http://somedomain.com/#/searchresults/?lastname=king&firstname=stephen&city=somecity

如何網址是池莉構建在此不再贅述的方式。在我的情況下,我用表單和一些事件處理程序使用我自己的視圖。

把我弄得工作看起來像這樣的代碼:

App.Router.map(function() { 
    this.resource("searchresults", { path: '/searchresults/:dynamic' }); 
}); 

App.SearchresultsRoute = Ember.Route.extend((function() { 
    var deserializeQueryString = function (queryString) { 
     if(queryString.charAt(0) === "?") 
      queryString = queryString.substr(1); 

     var vars = queryString.split('&'), 
      i = 0, length = vars.length, 
      outputObj = {}; 

     for (; i < length; i++) { 
      var pair = vars[i].split('='); 
      outputObj[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); 
     } 
     return outputObj; 
    }; 

    return { 
      model: function(param) { 
      var paramObj = deserializeQueryString(param.dynamic); 
      return App.Searchresult.find(paramObj); 
      } 
     }; 
    })() 
); 

App.Store = DS.Store.extend({ 
    revision: 12, 
    adapter: DS.RESTAdapter.create({ 
     namespace: 'api' 
    }) 
}); 

App.Searchresult = DS.Model.extend({ 
    lastname: DS.attr('string'), 
    firstname: DS.attr('string'), 
    street: DS.attr('string'), 
    hno: DS.attr('string'), 
    zip: DS.attr('string'), 
    city: DS.attr('string'), 
    country: DS.attr('string'), 
    birthdate: DS.attr('string') 
}); 

這產生一個HTTP GET請求,我的REST API:

{"searchresults": 
    [{"id":"2367507","lastname":"King","firstname":"Stephen","street":"Mainstreet.","hno":"21" ...}, 
    {"id":"3222409","lastname":"King","firstname":"Stephen","street":"Broadway","hno":"35" ...} 
    ]} 

http://somedomain.com/api/searchresults?lastname=king&firstname=stephen&city=somecity 

我的REST API與響應

然後用這個模板將其顯示出來:

<h2>Searchresults</h2> 
<table> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Street/Hno</th> 
      <th>City</th> 
      <th>Birthyear</th> 
     </tr> 
    </thead> 
    <tbody> 
    {{#each item in controller}} 
     <tr> 
      <td>{{item.firstname}} {{item.lastname}}</td> 
      <td>{{item.street}} {{item.hno}}</td> 
      <td>{{item.zip}} {{item.city}}</td> 
      <td>{{item.birthdate}}</td> 
     </tr> 
    {{/each}} 
    </tbody> 
</table> 

如果有人發現更優雅的方式,那不需要使用自定義的解串器,我會很樂意更新解決方案。 (另一個)丹尼爾提供的答案暗示http://somedomain.com/#/searchresults/king/stephen/somecity不適合我的情況,因爲在我需要的解決方案中,我有超過10個不同的搜索標準/過濾器。用戶通常只選擇填寫其中的一些。

這個例子上燼數據修訂的基礎:12和灰燼1.0.0-RC.3

0

我有網址,其中包含有關新聞頁碼的信息,並根據它發送和ajax調用。我的路由器是這樣的:

App.Router.map(function() { 
    this.resource('news', { path: '/n/:id' }); 
}); 

但你也可以不喜歡它:

App.Router.map(function() { 
     this.resource('searchresults', { path: '/searchresults/:lastname/:firstname/:city' }); 
    }); 

以及您的網址應該是這樣的:

http://localhost/#/searchresults/king/stephen/somecity 

然後你只需指定:

App.SearchresultsRoute = Em.Route.extend({ 
    model: function(params) { 
     var lastname = params.lastname; 
      var firstname = params.firstname; 
      var city = params.city; 
      someFunction(lastname, firstname, city); 
    } 
}); 

我希望我的回答有幫助y OU。祝你今天愉快!

+0

這可能會實現。但是,我的實際模型和我的搜索表單比較複雜。用戶可以選擇10個以上的字段(名字,姓名,街道,住宅號碼,出生日期,...)。爲了進行搜索,用戶通常僅爲這些字段中的幾個提供內容。如果我要這樣構造URL,他們總是必須明確地包含所有的標準,即使這些字段中的大部分都是空的:即http:// localhost /#/ searchresults/king/stephen //////// somecity。我不喜歡,這個網址不能「顯示」某個位置代表什麼。即它不可見,那個位置3代表街道。 – Daniel 2013-05-12 11:37:28

3

要利用燼數據,你可以這樣做。 假設你有一個這樣的模式:

App.SearchResult = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    city: DS.attr('string') 
}); 

,然後你在你的路線模型掛鉤是做你這樣做:

App.SearchResultsRoute = Ember.Route.extend({ 
    model: function(params){ 
    return App.SearchResult.find({firstName:params.firstName,lastName:params.lastName,city:params.city}) 
    } 
}); 

這樣的RESTAdapter會自動發出像你的請求。例如:

http://localhost/#/searchresults?firstname=xxx&lastname=xxx&city=xxx 

希望它可以幫助

+0

我試過這個,但是我得到這個錯誤:「未捕獲的錯誤:沒有路由與URL匹配'/ searchresults?name = anna'」。我的App.Router.map包含「his.resource('searchresults');」 – Daniel 2013-05-12 11:28:10

+0

我以爲你已經是動態細分市場了:'this.resource('searchresults',{path:'/ searchresults /:dynamic'});' – intuitivepixel 2013-05-12 11:40:11

+0

太棒了。這樣我就能使它工作。將會有兩個區別:網址現在是...... ts /?firstname = x ...在「?」之前帶有短劃線。 2.在SearchResultsRou​​te中傳遞給model()的參數並不像您的示例中所示的那樣結構化。相反,我得到{dynamic:「?name = anna」}。所以另外,我將不得不反序列化查詢字符串。我可以輕鬆處理。 – Daniel 2013-05-12 12:47:18