简体   繁体   中英

How to use LSAdapter in Ember?

My integration of LSAdapter looks like this:

index.html

...
<script src="lib/ember-1.6.1.js"></script>
<script src="lib/ember-data.js"></script>
<script src="lib/localstorage_adapter.js"></script>
<script src="src/app.js"></script>
...

app.js

...
App = Ember.Application.create();
App.ApplicationSerializer = DS.LSSerializer.extend();
App.ApplicationAdapter = DS.LSAdapter.extend({
    namespace: 'My-Data'
});
App.ApplicationStore = DS.Store.extend({
    adapter: App.ApplicationAdapter
});
...

When I debug my createRecord() call it never goes to the localstorage_adapter.js , just the createRecord() implementation of ember-data.js is called. So the data is in-memory but never hits localStorage .

Am I missing anything not mentioned in the docs of LSAdapter or Ember-Data?

All of the persistence adapters work in the same way. You create a record in memory with createRecord() , and then persist it with save() .

// First create the record in memory
var myRecord = this.store.createRecord('model-name', {
  attribute1: 'value1',
  attribute2: 'value2'
});

// Then persist the record to storage
myRecord.save();

The docs have a great walkthrough of building an app using Fixtures, and then they switch to the local storage adapter at the end. It's more or less a drop-in replacement and there's nothing specific to make it work with local storage. Check out the example on "Accepting Edits" to see how they are using save() to persist the record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM