Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for return select options with promises. #231

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions backbone.stickit.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,42 +592,52 @@
optList = list;
}

// Support Backbone.Collection and deserialize.
if (optList instanceof Backbone.Collection) optList = optList.toJSON();

if (selectConfig.defaultOption) {
addSelectOptions(["__default__"], $el);
}

if (_.isArray(optList)) {
addSelectOptions(optList, $el, val);
} else if (optList.opt_labels) {
// To define a select with optgroups, format selectOptions.collection as an object
// with an 'opt_labels' property, as in the following:
//
// {
// 'opt_labels': ['Looney Tunes', 'Three Stooges'],
// 'Looney Tunes': [{id: 1, name: 'Bugs Bunny'}, {id: 2, name: 'Donald Duck'}],
// 'Three Stooges': [{id: 3, name : 'moe'}, {id: 4, name : 'larry'}, {id: 5, name : 'curly'}]
// }
//
_.each(optList.opt_labels, function(label) {
var $group = Backbone.$('<optgroup/>').attr('label', label);
addSelectOptions(optList[label], $group, val);
$el.append($group);
var addList = function(listToAdd) {
// Support Backbone.Collection and deserialize.
if (listToAdd instanceof Backbone.Collection) listToAdd = listToAdd.toJSON();

if (_.isArray(listToAdd)) {
addSelectOptions(listToAdd, $el, val);
} else if (listToAdd.opt_labels) {
// To define a select with optgroups, format selectOptions.collection as an object
// with an 'opt_labels' property, as in the following:
//
// {
// 'opt_labels': ['Looney Tunes', 'Three Stooges'],
// 'Looney Tunes': [{id: 1, name: 'Bugs Bunny'}, {id: 2, name: 'Donald Duck'}],
// 'Three Stooges': [{id: 3, name : 'moe'}, {id: 4, name : 'larry'}, {id: 5, name : 'curly'}]
// }
//
_.each(listToAdd.opt_labels, function(label) {
var $group = Backbone.$('<optgroup/>').attr('label', label);
addSelectOptions(listToAdd[label], $group, val);
$el.append($group);
});
// With no 'opt_labels' parameter, the object is assumed to be a simple value-label map.
// Pass a selectOptions.comparator to override the default order of alphabetical by label.
} else {
var opts = [], opt;
for (var i in optList) {
opt = {};
opt[selectConfig.valuePath] = i;
opt[selectConfig.labelPath] = optList[i];
opts.push(opt);
}
opts = _.sortBy(opts, selectConfig.comparator || selectConfig.labelPath);
addSelectOptions(opts, $el, val);
}
};

if (_.isFunction(optList.then)) {
optList.then(function(promisedList){
addList(promisedList);
});
// With no 'opt_labels' parameter, the object is assumed to be a simple value-label map.
// Pass a selectOptions.comparator to override the default order of alphabetical by label.
} else {
var opts = [], opt;
for (var i in optList) {
opt = {};
opt[selectConfig.valuePath] = i;
opt[selectConfig.labelPath] = optList[i];
opts.push(opt);
}
opts = _.sortBy(opts, selectConfig.comparator || selectConfig.labelPath);
addSelectOptions(opts, $el, val);
addList(optList);
}
},
getVal: function($el) {
Expand Down
27 changes: 27 additions & 0 deletions test/bindData.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,33 @@ test('bindings:selectOptions:defaultOption:OptGroups', 8, function() {
equal(model.get('water'), 'dasina');
});

test('bindings:selectOptions (Promise)', function() {

var collection = new Backbone.Collection([{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}]);
var deferred = $.Deferred().resolveWith(this, [collection]);

model.set({'water':'fountain'});
view.model = model;
view.templateId = 'jst8';
view.bindings = {
'#test8': {
observe: 'water',
selectOptions: {
collection: function() { return deferred; },
labelPath: 'name',
valuePath: 'name'
}
}
};
$('#qunit-fixture').html(view.render().el);

stop();
deferred.then(function(){
equal(getSelectedOption(view.$('#test8')).data('stickit_bind_val'), 'fountain');
start();
});
});

test('bindings:selectOptions (collection path relative to `this`)', function() {

view.collection = new Backbone.Collection([{id:1,name:'fountain'}, {id:2,name:'evian'}, {id:3,name:'dasina'}]);
Expand Down