Skip to content

Commit

Permalink
add support for ngModelOptions, fix #111
Browse files Browse the repository at this point in the history
  • Loading branch information
lordfriend committed Jan 30, 2016
1 parent 3236327 commit 7e7d19b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
15 changes: 15 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,21 @@ <h3>ngSwitch</h3>
</div>

</form>

<h3>ngModelOptions</h3>
<p>should work with ngModelOptions</p>

<form class="form-inline">
<ol id="ngModelOptionsExample" class="nya-bs-select" ng-model="model9" multiple ng-model-options="{updateOn: 'blur', debounce: 200}">
<li nya-bs-option="option in options2">
<a>
{{option}}
<span class="glyphicon glyphicon-ok check-mark"></span>
</a>
</li>
</ol>
<div class="form-static-control">{{model9}}</div>
</form>
</section>
</div>
<footer class="footer">
Expand Down
30 changes: 15 additions & 15 deletions src/nya-bs-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', 'nyaBsC
if(isDisabled) {
return;
}

console.log('dropdown click');
if(jqLite(event.target).hasClass('dropdown-header')) {
return;
}
Expand Down Expand Up @@ -692,16 +692,16 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', 'nyaBsC
return;

var liElements,
modelValue,
wv,
viewValue;

liElements = dropdownMenu.find('li');
if (liElements.length > 0) {
modelValue = ngCtrl.$modelValue;
wv = ngCtrl.$viewValue;

// make a deep copy enforce ngModelController to call its $render method.
// See: https://github.com/angular/angular.js/issues/1751
viewValue = Array.isArray(modelValue) ? deepCopy(modelValue) : [];
viewValue = Array.isArray(wv) ? deepCopy(wv) : [];

for (var i = 0; i < liElements.length; i++) {
var nyaBsOption = jqLite(liElements[i]);
Expand Down Expand Up @@ -744,7 +744,7 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', 'nyaBsC
function selectOption(nyaBsOption) {
var value,
viewValue,
modelValue = ngCtrl.$modelValue,
wv = ngCtrl.$viewValue,
index;
// if user specify the value attribute. we should use the value attribute
// otherwise, use the valueIdentifier specified field in target scope
Expand All @@ -755,7 +755,7 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', 'nyaBsC
if(isMultiple) {
// make a deep copy enforce ngModelController to call its $render method.
// See: https://github.com/angular/angular.js/issues/1751
viewValue = Array.isArray(modelValue) ? deepCopy(modelValue) : [];
viewValue = Array.isArray(wv) ? deepCopy(wv) : [];
index = indexOf(viewValue, value);
if(index === -1) {
// check element
Expand Down Expand Up @@ -827,19 +827,19 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', 'nyaBsC
}

function updateButtonContent() {
var modelValue = ngCtrl.$modelValue;
var viewValue = ngCtrl.$viewValue;
$element.triggerHandler('change');

var filterOption = dropdownToggle.children().eq(0);
if(typeof modelValue === 'undefined') {
if(typeof viewValue === 'undefined') {
/**
* Select empty option when model is undefined.
*/
filterOption.empty();
filterOption.append(getDefaultNoneSelectionContent());
return;
}
if(isMultiple && modelValue.length === 0) {
if(isMultiple && viewValue.length === 0) {
filterOption.empty();
filterOption.append(getDefaultNoneSelectionContent());
} else {
Expand All @@ -862,14 +862,14 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', 'nyaBsC
}

// data-selected-text-format="count" or data-selected-text-format="count>x"
if((typeof count !== 'undefined') && modelValue.length > count) {
if((typeof count !== 'undefined') && viewValue.length > count) {
filterOption.empty();
if(localizedText.numberItemSelectedTpl) {
filterOption.append(jqLite(localizedText.numberItemSelectedTpl.replace('%d', modelValue.length)));
filterOption.append(jqLite(localizedText.numberItemSelectedTpl.replace('%d', viewValue.length)));
} else if(localizedText.numberItemSelected) {
filterOption.append(document.createTextNode(localizedText.numberItemSelected.replace('%d', modelValue.length)));
filterOption.append(document.createTextNode(localizedText.numberItemSelected.replace('%d', viewValue.length)));
} else {
filterOption.append(document.createTextNode(modelValue.length + ' items selected'));
filterOption.append(document.createTextNode(viewValue.length + ' items selected'));
}
return;
}
Expand All @@ -882,7 +882,7 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', 'nyaBsC
value = getOptionValue(nyaBsOption);

if(isMultiple) {
if(Array.isArray(modelValue) && contains(modelValue, value)) {
if(Array.isArray(viewValue) && contains(viewValue, value)) {
// if option has an title attribute. use the title value as content show in button.
// otherwise get very first child element.
optionTitle = nyaBsOption.attr('title');
Expand All @@ -894,7 +894,7 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', 'nyaBsC

}
} else {
if(deepEquals(modelValue, value)) {
if(deepEquals(viewValue, value)) {
optionTitle = nyaBsOption.attr('title');
if(optionTitle) {
selection.push(document.createTextNode(optionTitle));
Expand Down

0 comments on commit 7e7d19b

Please sign in to comment.