From 8c050fa8724c7eb666a962a7df2fbabe619b9a92 Mon Sep 17 00:00:00 2001 From: pradius-fut Date: Wed, 23 Apr 2014 14:17:00 +0200 Subject: [PATCH 1/2] Added support for return select options with promises. Checks for .then function as all promises including jQuery deffered are 'thenable' --- backbone.stickit.js | 68 ++++++++++++++++++++++++++------------------- test/bindData.js | 27 ++++++++++++++++++ 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/backbone.stickit.js b/backbone.stickit.js index 89a6f76..a85d4d1 100644 --- a/backbone.stickit.js +++ b/backbone.stickit.js @@ -536,41 +536,51 @@ else if (_.isFunction(list)) optList = applyViewFn(this, list, $el, options); else 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.$('').attr('label', label); - addSelectOptions(optList[label], $group, val); - $el.append($group); + var doAddList = 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.$('').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 listToAdd) { + opt = {}; + opt[selectConfig.valuePath] = i; + opt[selectConfig.labelPath] = listToAdd[i]; + opts.push(opt); + } + addSelectOptions(_.sortBy(opts, selectConfig.comparator || selectConfig.labelPath), $el, val); + } + }; + + if (_.isFunction(optList.then)) { + optList.then.call(this, function(promisedList){ + doAddList(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); - } - addSelectOptions(_.sortBy(opts, selectConfig.comparator || selectConfig.labelPath), $el, val); + doAddList(optList); } }, getVal: function($el) { diff --git a/test/bindData.js b/test/bindData.js index 5cc499b..8cbcd16 100644 --- a/test/bindData.js +++ b/test/bindData.js @@ -735,6 +735,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'}]); From 66574d95954aded01299844f7d51948169261c27 Mon Sep 17 00:00:00 2001 From: pradius-fut Date: Thu, 1 May 2014 23:40:31 +0200 Subject: [PATCH 2/2] Merged master and modified some because of comments --- backbone.stickit.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/backbone.stickit.js b/backbone.stickit.js index 388d759..e9b196d 100644 --- a/backbone.stickit.js +++ b/backbone.stickit.js @@ -596,7 +596,7 @@ addSelectOptions(["__default__"], $el); } - var doAddList = function(listToAdd) { + var addList = function(listToAdd) { // Support Backbone.Collection and deserialize. if (listToAdd instanceof Backbone.Collection) listToAdd = listToAdd.toJSON(); @@ -620,24 +620,24 @@ // 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); + 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.call(this, function(promisedList){ - doAddList(promisedList); + optList.then(function(promisedList){ + addList(promisedList); }); } else { - doAddList(optList); + addList(optList); } }, getVal: function($el) {