2

Here is my Ember code: http://pastebin.com/Hb9HCyy6

Here is html: http://pastebin.com/kp195dFA

JSFiddle: http://jsfiddle.net/MBmUs/270/

Now even that stopped working lol I put that API online, when I access it with browser it works.

App.Store = DS.Store.extend({
        revision:12,
        adapter:DS.RESTAdapter.extend({
                url:"http://galdikas.net/rest_api/book-store-restful-api/api"
        })
});

Dont even know what to look for. But basically lets say I click on the "Admin area link", it takes me to the relevant area. But if I click back to "Back to store link", all the old data remains there, and model fetches same data again, and appends it to the old data. How would I tell it either not fetch any data if there is no new. Or if that is not possible, how would I tell it to simply overwrite the old data??

MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
galdikas
  • 1,609
  • 5
  • 19
  • 43
  • 2
    Do you mind putting it into a jsbin/jsfiddle, it makes it much easier to debug thanks – Bradley Priest Apr 06 '13 at 15:59
  • Done. But it doesn't work on JSFiddle. Maybe because of the redirects to "different" URLS? – galdikas Apr 06 '13 at 23:33
  • @BradleyPriest I added it to my server. here is linK: http://galdikas.net/rest_api/bookstore-app-emberjs/ Changed the links to handlebars code as suggested by sly7_7 .. it did not fix problem though. – galdikas Apr 07 '13 at 00:38

1 Answers1

2

Not sure, but I would use {{#linkTo 'index'}}Back To shop{{/linkTo}} instead of using the a tag. Same thing for linking to the admin session.

UPDATE: After "quick discussion", it seems the only "mistake" was an erroneous json payload send by the backend. By default, ember-data expect to have an 'id' as the primary key in order to correctly load the record into the store.

If the backend is immutable, you can configure ember-data to accept any key as the primary key. in this example:

DS.RESTADapter.map('App.Book', { primaryKey: 'book_id'})

sly7_7
  • 11,961
  • 3
  • 40
  • 54
  • Tried it. Unfortunately gives me 2 errors. _Uncaught Error: assertion failed: The route #/admin was not found ember-1.0.0-rc.2.js:52_ **Uncaught Error: assertion failed: Emptying a view in the inBuffer state is not allowed and should not happen under normal circumstances. Most likely there is a bug in your application. This may be due to excessive property change notifications.** And if i am at "#/admin" url, it give same error, except it says that #/book route is not found. Wtf? :D – galdikas Apr 06 '13 at 23:34
  • ok, got the links working. Unfortunately still have the same problem of data being appended to old data. – galdikas Apr 07 '13 at 00:19
  • I don't know, now for me doesn't fetch any data at all? I transferes it to my pc, and chrome dev tools diesnt even show any requests :( – galdikas Apr 07 '13 at 12:19
  • Oh, my bad, I've just updated the fiddle, updating with the 'books' route everywhere. It does'nt work in the fiddle because of cross site reference, but this should work for you. see http://jsfiddle.net/ckLzF/ – sly7_7 Apr 07 '13 at 17:16
  • YEah it works now... but still keeps fetching new data, and appending it to old one... This is like my first attempt at making this kind of stuff, and it looks like ember js is specifically designed to make me feel less inteligent than I am lol Maybe I should start with something simpler? What framework would me more n00b friendly? :) – galdikas Apr 07 '13 at 17:57
  • I think I don't understand what you mean by "still keeps fetching new data, and appending it to old one". Anyway, here is an other attempt, using fixtures (and FixtureAdapter). Is this behavior ok or not for you ? http://jsfiddle.net/Kfxfk/ – sly7_7 Apr 07 '13 at 19:32
  • I've put online here, so the RESTful adapter works.Here is link:[link](http://galdikas.net/rest_api/bookstore-app-emberjs/). So click on books link, then on Admin link. And then on book again. What will happen is that all the books will still be there, and the list again will be fetched, and appended to old ones :) – galdikas Apr 07 '13 at 21:48
  • Hum, this is really weird... Would you mind to show me the whole code ? – sly7_7 Apr 08 '13 at 07:00
  • Yeah, I mean, the templates, the js, and the api please. This should not differ from what I did with the fixture adapter. Perhaps the Json payload is a little bit different from which is expected by ember-data. – sly7_7 Apr 08 '13 at 08:41
  • well the API is written with CodeIgniter, I used RESTFul controller written by Phil Sturgeon [link](https://github.com/awhitney42/codeigniter-restserver-resources/blob/master/application/libraries/REST_Controller.php). And you can see the JSON payload here: [link](http://galdikas.net/rest_api/book-store-restful-api/api/books/format/json). (format/json is just there so it return JSON instead of XML) – galdikas Apr 08 '13 at 08:49
  • Well, if you can, change your json by replacing book_id with id. If it's not possible, you can configure this by calling DS.RESTAdapter.map('App.Book',{primaryKey: 'book_id'}) before the Store creation. – sly7_7 Apr 08 '13 at 09:28
  • This worked... dont know how the f*ck does that make any difference but it worked. Change your answer to something that reflects that, and ill upvote and accept it, that least I can do :D Thanks a lot!!!!! – galdikas Apr 08 '13 at 09:41
  • 1
    Well, that's "simple", out of the box, ember-data expects to have 'id' as a the primary key in order to correctly extract the data and load into the store. But clearly, here, I think there should at least be a warning or something like that. – sly7_7 Apr 08 '13 at 09:52
  • It seems that it does a lot of things without asking. May be good for folks that are experienced in this sort of thing, and expect certain things to work in certain ways. And for someone new like me, it may be a major drawback, like this example illustrates :D I would of never thought, that the name of id would (or should) make any difference, but here you go :D – galdikas Apr 08 '13 at 09:56
  • You have some very tiny example in the docs http://emberjs.com/guides/models/the-rest-adapter/, but definitely, there should be more. Maybe it could help: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/serializer.js#L104 – sly7_7 Apr 08 '13 at 10:08