1

I am looking to retrieve a specific piece of data from Backbone (I'm using the localStorage adapter, not a server). I can see the ID of the items by looking at inspector.

I want a way to click on any item in any view, regardless of the collection it is in, or if it's it's own model, or whatever, and pass it to a details template.

So, <span onclick="router.navigate('details/:id', {trigger: true});">Single Page</span> would be attached in certain places throughout the app. All items would create the URL with their own ID.

Is it possible to agnostically render a template with the data from that single model?

My router:

    assetDetail: function () {

        var assetId = router.current().params[0];

        var assetModel = new app.Models.Show({
            id: assetId
        });

        var assetDetailView = new app.Views.assetDetail({
            model: assetModel
        });

    },

However, when I put console.log( this.model ); in my detail page's render function the data is in the right format, but everything is null (the model defaults).

Thank you for any help.

Update:

As long as I know the exact collection, I can do the following. I'm currently rewriting my app to be like this. However, it still would be great to know if you can do it without knowing the collection (just having the ID)

        var assetId = router.current().params[0];

        var collection = new app.Collections.Collection({});
        collection.fetch();

        var themodel = collection.get(assetId);

        var assetDetailView = new app.Views.assetDetail({
            model: themodel
        });
paintedbicycle
  • 287
  • 3
  • 13
  • I haven't ever used multiple collections of the same kind in my apps, so I haven't ever needed to distinguish them by id. For what you're trying to accomplish, however, you could use, for example ```assetCollection.get(assetId)``` to get the model instance, and output its ```properties``` key to get the clean object. – ffflabs Aug 17 '14 at 01:23
  • Now, for this, in my render function, I would need to know the collection that the "id" in the URL belongs to, no? How would I get around that? Also, sorry, what do you mean "output its properties key to get the clean object.", can you explain the process here? – paintedbicycle Aug 17 '14 at 01:24
  • @amenadiel If I change it so there is only one collection, would I have to instantiate a new version of the collection when doing this? I would imagine so, since I cannot access the variable for the collection in another function...but I don't know if creating a new collection just to be able to access the data in the old collection makes sense. Thanks! – paintedbicycle Aug 17 '14 at 16:01
  • I don't believe you need to instantiate another collection, you just need the reference to the actual collection instance. With that reference and your model ID, you can get the model properties, which is what you're trying to access in your original question. – ffflabs Aug 17 '14 at 16:31
  • @amenadiel Are you referring to the original var thisCollection = new app.Collection.ColName when I set up the collection and load in the json file? Because if so, that is instantiated but then can't be accessed in another view. Or is there another way to access that collection later? – paintedbicycle Aug 17 '14 at 16:35
  • @amenadiel Updated my question with your suggestion of relying on the collection. It works. If you would like to write up an answer I can select it. – paintedbicycle Aug 17 '14 at 16:51

0 Answers0