REST適配器確定按名稱與模型通信的URL。
Action | HTTP Verb | URL |
---|---|---|
Find | GET | /URLrepr/123 |
Find All | GET | /URLrepr |
Update | PUT | /URLrepr/123 |
Create | POST | /URLrepr |
Delete | DELETE | /URLrepr/123 |
store.find('post', 1).then(function(post){ //do the stuff });
<!DOCTYPE html> <html> <head> <title>Emberjs URL Conventions</title> <!-- CDN's --> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script> <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://builds.emberjs.com/release/ember.debug.js"></script> <script src="https://builds.emberjs.com/beta/ember-data.js"></script> </head> <body> <script type="text/x-handlebars" data-template-name="fruits"> <h1>Namespace is: <small>{{namespace}}</small></h1> <h2>Fruit names: </h2> {{#each fru in model}} <p><b>{{fru.title}}</b></p> {{/each}} </script> <script type="text/javascript"> Fruits = Ember.Application.create(); //Creating A Fixture Adapter Fruits.ApplicationAdapter = DS.FixtureAdapter.extend(); Fruits.Router.map(function () { this.resource('fruits', { path: '/' }); }); Fruits.FruitsRoute = Ember.Route.extend({ model: function () { return this.store.find('fru'); } }); Fruits.Fru = DS.Model.extend({ //data model title: DS.attr('string') }); //Endpoint Path Customization Fruits.FruitsAdapter = DS.RESTAdapter.extend({ namespace: 'api' }); //Attach Fixtures To The Model Class Fruits.Fru.FIXTURES = [{ id: 1, title: 'Apple'},{ id: 2, title: 'Orange'},{ id: 3, title: 'Graps' }]; </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在model_url_conven.html文件中
在瀏覽器中打開此HTML文件。
更多建議: