Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have conventional way of preloading data into the store #705

Closed
tomdale opened this issue Feb 7, 2013 · 12 comments
Closed

Have conventional way of preloading data into the store #705

tomdale opened this issue Feb 7, 2013 · 12 comments
Assignees
Labels
🏷️ feat This PR introduces a new feature
Milestone

Comments

@tomdale
Copy link
Member

tomdale commented Feb 7, 2013

Often, developers want to include a batch of data with the initial HTML request for the application, so that an additional "data-only" XHR isn't required to boot the app.

We should provide a convention for doing this.

@ghost ghost assigned tomdale Aug 10, 2013
@nragaz
Copy link
Contributor

nragaz commented Sep 4, 2013

Any progress on this one? My old (hackish) way of bootstrapping data no longer works with b1, so before I hack together a new way a conventional solution would be nice... push is useful but normalizing the data before pushing it still requires some work.

@sly7-7
Copy link
Contributor

sly7-7 commented Sep 4, 2013

@nragaz there is currently an other issue about normalizing during push: #1199 (comment)

@alexspeller
Copy link
Contributor

@tomdale if @bradleypriest's code in #1199 (below) was made into a tested PR (in javascript no less!) would you accept?

App.Store = DS.Store.extend  
  pushData: (prop, data) ->
    type       = @modelFor(prop)
    serializer = @serializerFor(type)
    if Ember.typeOf(data) is "array"
      data = data.map (object) -> serializer.normalize(type, prop, object)
      @pushMany(type, data)
    else
      data = serializer.normalize(type, prop, data)
      @push(type, data)

@alexspeller
Copy link
Contributor

Also, I don't know if this is intentional or not, but pushing into the store does not push the object into hasMany arrays. Is this expected behaviour? Should adding a new object to the store also push into a hasMany array? Anyone have any info on this?

@zeevallin
Copy link

@alexspeller - This works fine for me running on latest ember-data. Check the TRANSITION.md about changes made to relations and response serialisation.

@alexspeller
Copy link
Contributor

@Zeeraw are you talking about hasMany array being a live array? I've read the transition docs very carefully and it is still not working.

@zeevallin
Copy link

@alexspeller I was referring to extractSingle

el  = 'body'
key = 'session'

payload = JSON.parse($(el).attr(key))
$(el).attr(key,null)

record_id = payload[key].id

store   = container.lookup('store:main')
factory = store.modelFor(key)
adapter = store.adapterForType(key)

store.push(key, adapter.get('serializer').extractSingle(store, factory, payload))

@sandstrom
Copy link
Contributor

There is now a method to push data into the store, which is awesome 4bf1b7a.
A related issue is avoiding xhr's when the store already has all the records [preloaded].

Example
We have a static set of countries packaged in our monolith.js, loaded using this method (to avoid an extra xhr). However, store.find('countries') still fire off an xhr. A way of telling the store that it already has up-to-date records would be useful.

In our case this static data is never persisted, but there are certainly use cases where data is preloaded but still need to be modified.

Possible solutions

  1. Using some 'expiresAt' value for the records (which could be given a very high value for practical non-expiry).
  2. By simply marking records/models as 'local' in some sense. This could be either on the query level (options to find) or on the adapter level (all the time). Either way, there would be no attempt to fetch from the adapter, the store would only work with what it already had.
  3. Using a custom adapter for the preloaded models, where find returns/resolves instantly. That works fine for models that need not be persisted (read-only), but is probably less useful for data that is preloaded, but where subsequent finds/reloads from the server may still be needed.

@alexspeller
Copy link
Contributor

@sandstrom doesn't store have .all() which has the behaviour you want, I.e. return all records currently in the store without making a request and updating the array returned if more records arrive? Or is that gone now?

@sandstrom
Copy link
Contributor

@alexspeller It's still around, thanks!!

@alexspeller
Copy link
Contributor

@tomdale see #1309

@wycats
Copy link
Member

wycats commented Sep 22, 2013

I'm closing this due to #1309. It might be nice to have <script type="text/x-bootstrap-payload"> that "just works". Anyone want to grab it?

@wycats wycats closed this as completed Sep 22, 2013
@runspired runspired added 🏷️ feat This PR introduces a new feature and removed Improvement labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ feat This PR introduces a new feature
Projects
None yet
Development

No branches or pull requests

8 participants