Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore.js _.partial with _ as argument not working

I'm using Backbone and Underscore.

According to Underscore.js's documentation for the _.partial(function, *args) function:

You may pass _ in your list of arguments to specify an argument that should not be pre-filled, but left open to supply at call-time.

Does anyone have a working example of this? I am creating the partial call within a view like this:

this.filterCollection = _.partial(filterFn, _, searchTerm);

Where filterFn looks like:

function(license, key) {
    var organization;
    organization = App.organizations.get(license.get('organization_id'));

    if (license.isNew()) {
        return true; // Always include new models in the search.
    } else {
        return (organization && organization.get('name').indexOf(key) !== -1);
    }
}

I'm calling it in the view like this:

this.filterCollection(model)

The filterFn crashes on the line: organization = App.organizations.get(license.get('organization_id')); because license has no method get.

When I inspect license in Chrome, it points to:

Object function (obj) {
    if (obj instanceof _) return obj;
    if (!(this instanceof _)) return new _(obj);
    this._wrapped = obj;
}
like image 819
brokethebuildagain Avatar asked Jul 09 '26 22:07

brokethebuildagain


1 Answers

Looks like they just added the option to use _ as an argument in version 1.6.0. No wonder I couldn't find much documentation on it. :)

like image 100
brokethebuildagain Avatar answered Jul 14 '26 03:07

brokethebuildagain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!