简体   繁体   中英

How to transition record from “created.inFlight” to “saved” w/ Ember Data & pouchdb adapter

I am building an app with an input form for a record with a belongsTo relationship. I'm using Ember Data and pouchdb ( https://github.com/nolanlawson/ember-pouch ).

Let's say I want a banana to belong to a basket.

// new banana controller

banana.set('basket', basketRecord);

banana.save();

After that I transition to the banana route, where I want to display the created record.

// banana.hbs

<p>{{basket.title}}</p>

The title above only shows up, after I reload the page, also the record identifies as created.inFlight after calling save() from the banana controller, so I can't reload the record manually.

From my understanding, a record transitions to the saved -state, after the saved model has been synced to the server (or, in this case, my PouchDB).

How can I force my model to update so that the record won't be inFlight anymore?

Is it even a good idea to force this or should I just wait for some state transition event to happen and proceed after that?

Thanks in advance and also big shout-outs to the creators and community of Ember, Ember CLI and PouchDB for empowering noobs like me to build awesome things.

So this is part of a more complex question, which is:

How can I detect in my local app that changes have been synced from the local database to the remote database?

This is actually not terribly hard to determine, but you just have to know where to ask.

Basically, you're going to want to listen for changes on the remote CouchDB, something like:

new PouchDB('http://somewhere:5984/somedb')
    .changes({live: true}).on('change', function (change) {
  console.log('yo, got a change');
});

That change object will contain an id which you can then map back to the original database. Unfortunately this is where the relational-pouch abstraction starts to leak a little bit, because I never wrote a changes() wrapper for relational-pouch , so you'll have to do it yourself. Here's an example .

Then at that point you will also need to update Ember, and that thread contains some info about how to do it. Sorry that it's not any easier, but I hope that helps!

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