Giter Club home page Giter Club logo

Comments (9)

ebryn avatar ebryn commented on August 15, 2024

Decided to leave this up to people to implement themselves as it's tricky to build this generically.

from ember-model.

guilhermeaiolfi avatar guilhermeaiolfi commented on August 15, 2024

Could you show an code example that does that?

from ember-model.

alexspeller avatar alexspeller commented on August 15, 2024

At the very basic level, i guess:

  if(typeof model.get('foo') is 'undefined') { model.reload() }

from ember-model.

guilhermeaiolfi avatar guilhermeaiolfi commented on August 15, 2024

I was expecting something inside the model class, because usually I don't 'get' the model values.
And how my server will know when to deliver the full data response because reload will access the same URL as the first time.
I would like to pass the undefined property name to the server too so it knows what your trying to access and then you could return the record in more than two parts if you want to.

from ember-model.

drogus avatar drogus commented on August 15, 2024

@guilhermeaiolfi one way of doing this would be to check if you got all the attributes from server and set the "incomplete" flag (or whatever you wish to call it) based on that information. So for example if you have attributes title and description and you get hash with only title, you set it as incomplete. Then, when someone tries to access missing attribute (in this case description), you may reload the record.

This use case is often used with list and detailed view - you load part of the data for list and you load the rest when you do reload as it hits /things/1 URL. If you wish to pass a property to control if the endpoint returns full data, it will be trickier, as reload does not take arguments. In order to support this, you could implement modified version of reload.

Regrading the example implementation, in Travis I do:

  1. Override load, to set incomplete flag: https://github.com/travis-ci/travis-web/blob/d9d833d1b4f6933a88e4e0b93e12ea0101598b19/assets/scripts/lib/travis/model.coffee#L34-L62
  2. Override getAttr, getBelongsTo and getHasMany methods: https://github.com/travis-ci/travis-web/blob/d9d833d1b4f6933a88e4e0b93e12ea0101598b19/assets/scripts/lib/travis/model.coffee#L64-L78
  3. Load the rest of the record if it's needed: https://github.com/travis-ci/travis-web/blob/d9d833d1b4f6933a88e4e0b93e12ea0101598b19/assets/scripts/lib/travis/model.coffee#L81-L83

We get incomplete records from pusher, so I can rely on default reload, but other than that you could implement something similar.

from ember-model.

guilhermeaiolfi avatar guilhermeaiolfi commented on August 15, 2024

Thank you, @drogus. I'm starting to get the idea. Looking at your code, my first thought was to put needsCompletionCheck code in unknownProperty so the reload is done automatically every time the app tries to get something that doesn't exist in the model. You could then check for a list of valid properties and use custom logic to load.

What do you think?

from ember-model.

alexspeller avatar alexspeller commented on August 15, 2024

I would also add that personally I think this is semantically broken - you should always return the whole resource, and if you need partial resources you actually should have two resources - so for the title and description example, you would make a PostDescription model that would belong to the post model and load it separately.

It sounds like in your use case this would make even more sense than in @drogus's use case, because he at least is getting partial information via websocket and then always returning the full record from the REST endpoint. I feel like if you're returning partial information from the REST endpoint depending on a param that's not generally a great thing to do and will get really complex. But not everyone agrees with me on that so don't take my word as gospel!

from ember-model.

drogus avatar drogus commented on August 15, 2024

What do you think?

I think that using unknownProperty will not work, because the attributes are defined, they're just not set to any value yet. That's why we added hooks which get called on attributes or relationship access: getAttr, getBelongsTo and getHasMany

from ember-model.

jonnii avatar jonnii commented on August 15, 2024

@alexspeller the reason why partial records are nice is that if you do something like post.set('title', 'foo'), you'd also like to see that reflected in the post header. You can do this manually, but it can be a bit annoying, so in a case like that partial records would be nice. Perhaps what would make more sense is not a partial record, but something like a related record, so that the post header and the post are related by id.

from ember-model.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.