Work with records for a modal other than the current routes model

What would be the ember/ember-data way of creating a record for a record that is not the current routes record?

The scenario I have is I have an items route that finds is a list of items, when I click on one I transition to the item route which shows me all the info for that one item. I also have a bids resource but the way the bids will be worked with is always from an item route. So placing a bid happens on an item route.

So how would I go about tackling this? I was going to setup an ajax request in an action connected to my place bid button in the item route but I’m worried this might not be the ember way?

You can do something like this:

https://github.com/broerse/ember-cli-blog/blob/master/app/routes/posts.js

https://github.com/broerse/ember-cli-blog/blob/master/app/controllers/post.js

https://github.com/broerse/ember-cli-blog/blob/master/app/templates/post.hbs

Follow posts.authors

So what if I’m not going to display bids I’m just going to use them to update the item? It’s more like a silent auction idea so there is no need to show who is bidding on an item, more just that an item received a new bid so it’s bid amount needs updated. Could I stop with the RSVP.hash and not worry about setting up the controller or is all that still necessary to display the items?

If you just want to update the Item you can sent an action and add the bid data on handling that action. You can skip the bit: Ember.inject.controller() and skip sending the bid to a component.

Trying to explain this let you write strange sentences :wink:

Okay here is what I’ve tried and am getting an error.

In my item route where I’m also trying to do the save function here is my model I’m attempting

model(params) {
    var store = this.store;
    return Ember.RSVP.hash({
        content: store.findRecord('item',params.item_id),
        bids: store.findAll('bid')
    });
},

And in the console on the item route I see this

Error while processing route: item DS.default.Attr is not a function TypeError: DS.default.Attr is not a function
    at http://localhost:4200/assets/bidr.js:2162:28
    at mod.state (http://localhost:4200/assets/vendor.js:150:29)
    at tryFinally (http://localhost:4200/assets/vendor.js:30:14)
    at requireModule (http://localhost:4200/assets/vendor.js:148:5)
    at Ember.DefaultResolver.extend._extractDefaultExport (http://localhost:4200/assets/vendor.js:69229:20)
    at resolveOther (http://localhost:4200/assets/vendor.js:68961:32)
    at superWrapper (http://localhost:4200/assets/vendor.js:32770:20)
    at exports.default._emberRuntimeSystemObject.default.extend.resolve (http://localhost:4200/assets/vendor.js:15520:35)
    at Object.resolve [as resolver] (http://localhost:4200/assets/vendor.js:15276:23)
    at resolve (http://localhost:4200/assets/vendor.js:12768:29)

I looked up the error and found this stack overflow question

Do I need to make a serializer for the item to tell it what the primary key is?

If you’re curious here is what a bid looks like

{
    "id":5001,
    "user_id":697,
    "item_id":4107,
    "bid_amt":100,
    "bid_time":"2015-01-21 12:57:39",
    "status":0,
    "notified":true
}

Also it would be really nice to only pull up bids for that particular item rather than all the bids, since when this gets hooked up to a server it will be a lot of bids.

Why is assets/bidr.js a separate asset? Is your Bid model something like this?

import DS from "ember-data";

var Bid = DS.Model.extend({
   user_id: DS.attr('string'),
   item_id: DS.attr('string'),
   bid_amt: DS.attr('string'),
   bid_time: DS.attr('date'),
   status: DS.attr('string'),
   notified: DS.attr('string')
});

export default Bid;

I think you don’t need to do anything with your id.

I don’t know that’s what ember has been doing, I didn’t tell it otherwise to my knowledge

Yes that is my model for the bid

Strange it should work, is default there? export default Bid;

My model is structure like it does in ember cli 1.13.7

export default DS.Model.extend({
  ...
});

Is Ember 1.13.7 in Ember-cli 1.13.7 ? I have super strange things with Ember 1.13.7. Try changing to Ember 1.13.8 in case you now have 1.13.7

Model is OK like that.

Your params.item_id is referring to id in your model and you also have item_id in you model. It should not be a problem but can be confusing.