Trouble understanding plural route ("/photos") with single form model ("photo")


#1

Hi everyone,

So, I am new to Ember, and I’m trying to grasp how everything works together.

One area I’m struggling with is the proper handling of a list of elements. For instance, if I’m making a photo gallery, it’s pretty intuitive that I’m going to have a “photo” model for when the “/photo” route is accessed. What I’m struggling with is the “/photos” route. My gut tells me that I shouldn’t be creating a separate “photos” or “gallery” model unless there’s need for one (gallery name and/or description as example properties). In my case, there is no need for this, as there won’t be numerous galleries. I’m simply trying to display photos on the application index.

For now, my data lives in a JSON file that’s generated from another application. I retrieve the data in the model hook of a pluralized route (“entries”). There is an “entry” model. The JSON format doesn’t match the expected REST format, so I’m handling retrieval here. This is mostly for proof of concept. I wanted to utilize the store to see if I could cache the data and get some experience with Ember Data.

router.js

this.route('entries', { path: '/' }, function()
{
	this.route('entry');
});

routes\entries.js

model: function()
{
	var _this = this;

	$.getJSON('/data/entries.json', function(data)
	{
		for (var i = 0; i < data.length; i++)
		{
			var entry =
			{
				id: i + 1,
				slug: data[i].slug, 
				title: data[i].title,
				category: data[i].category,
				description: data[i].description,
				year: data[i].year
			};

			_this.store.createRecord('entry', entry);
		}
	});
}

I think there is more that I need to do here for caching and setting up the controller, but importantly this fails unless I create an “entries” model. It fails on store.createRecord when i == 0 with the following error: “Uncaught Error: No model was found for ‘entries’”

Again, generating an “entries” model at least makes the code work. However, when I inspect Ember Data with the Ember Chrome extension, I see that “entries” has 0 elements and “entry” is filled with appropriate data. From this, I have no use for the “entries” model (I don’t think?). It’s just somewhat of a “dummy model” and I feel like I’m doing this wrong. I suspect the answer to my woes has to do with ArrayControllers, but I can’t for the life of me figure out how to get it working from the example in the guide. :frowning:

I’d appreciate any insight on best practices here! Or even a link to a full example of a very similar setup. I’ve been spending a lot of time with the guide, but there’s a lot to consume. THANK YOU for your help!