Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass initialize options to Backbone Model through collection fetch method

I have a Backbone Model which I want to initialize with some options:

 Payment.Payment = Backbone.Model.extend

    initialize: (attributes, options) ->
        @user = options.user

These Payments are fetched from the server through a Payment Collection

# Collections: Payments
Payment.Payments = Backbone.Collection.extend
    model: Payment.Payment
    url: 'api/payments'

    initialize: (models, options) ->
        @user = if options?.user then options.user else app.user

When I try to fetch a set of payments from the server, however, I can't find a way to pass the user option to each of the Payment models:

payments = new Payment.Payments
payments.fetch()

I've tried passing in a user option as a parameter to the fetch call, but that doesn't work. How then can I instantiate all the payment models fetched from the server with the user?

like image 520
zimkies Avatar asked Apr 04 '13 14:04

zimkies


1 Answers

You should have tried the simplest:

payments.fetch user: payments.user
// have no idea what I'm doing

In JS ^^:

payments.fetch({user: payments.user});

The options you pass fetch will be given to the models.

like image 128
Loamhoof Avatar answered Oct 11 '22 20:10

Loamhoof