I’d like some help figuring out what seems to be one small missing piece from my learning experiment. I’m using the code that Robin Ward created for his recent blog post (thanks again Robin!). This code pulls back a list of reddit links and displays them in a tabular format. What I want to do is extend that with an input field that will allow a user to enter a subreddit name (e.g.: wtf) and have it refresh the page with the new data.
I’ve added an input field and button that will allow a user to enter a subreddit and I’ve gotten to the point where you can submit the value & the Ajax call is made but I’m not sure how to refresh the template with the new data.
Hoping someone could give me a little help here. I’m thinking it’s something minor that I’m missing.
It depends on when you want to clear the current data. If you set model in the loadList method, then your list will be emptied right away, and populated again once the ajax calls finishes. To have it this way, you need to let the findAll method return the links (which will be empty) right away. When the ajax request is finished the array will be filled, and your view will automatically be updated. It works like a promise. Example:
I don’t believe there are any differences, as long as you don’t mess with Ember.EXTEND_PROTOTYPES as mentioned in Ember - 4.6 - Ember API Documentation.
In Ember, you use .get, .set and <*>Object (pushObject, popObject, etc.) so that Ember can see the changes and fire observers.
You don’t need to use Ember.A() if prototype extensions are on (which they are by default), but code inside Ember doesn’t assume prototype extensions are on, so you’ll see a lot of Ember.A() inside of framework code.
The alternative to this approach is to deep-diff the array every time any app code runs, which has serious performance implications. At the moment, we have chosen to go with slightly more clunky APIs that Ember can observe directly.
There are plans in the platform (Object.observe, Array.observe, Proxy, etc.) to make it possible to use raw properties directly, and we plan to make use of them just as soon as all of the needed functionality lands in several browsers and is no longer obviously experimental.
For what it’s worth, I have heavily pushed for Object.observe (and array enhancements) on TC39, and I’ve been focusing on making sure that Proxy satisfies the “virtual object” use-case, so I’m very aware of (and working on) future solutions that will improve the Ember API. For now, it’s a tradeoff between an API that looks like POJOs (but requires deep diffing and dirty checking of all live bindings after user code) or a clunkier API that can be observed directly. Until there’s a third alternative (Object.observe or Proxy) we’ve chosen an API that is not a performance footgun.