Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bootstrap-multiselect
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
bootstrap-multiselect
Commits
bfa88e43
Commit
bfa88e43
authored
Feb 12, 2015
by
David Stutz
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #447 from Aaron-P/master
Improved knockout binding.
parents
614bd4b5
3f1b6be7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
54 deletions
+65
-54
bootstrap-multiselect.js
dist/js/bootstrap-multiselect.js
+65
-54
No files found.
dist/js/bootstrap-multiselect.js
View file @
bfa88e43
...
...
@@ -11,71 +11,82 @@
if
(
typeof
ko
!==
'
undefined
'
&&
ko
.
bindingHandlers
&&
!
ko
.
bindingHandlers
.
multiselect
)
{
ko
.
bindingHandlers
.
multiselect
=
{
after
:
[
'
options
'
,
'
value
'
,
'
selectedOptions
'
],
init
:
function
(
element
,
valueAccessor
,
allBindingsAccessor
,
viewModel
,
bindingContext
)
{
var
listOfSelectedItems
=
allBindingsAccessor
().
selectedOptions
;
var
config
=
ko
.
utils
.
unwrapObservable
(
valueAccessor
());
$
(
element
).
multiselect
(
config
);
if
(
isObservableArray
(
listOfSelectedItems
))
{
// Set the initial selection state on the multiselect list.
$
(
element
).
multiselect
(
'
select
'
,
ko
.
utils
.
unwrapObservable
(
listOfSelectedItems
));
// Subscribe to the selectedOptions: ko.observableArray
listOfSelectedItems
.
subscribe
(
function
(
changes
)
{
var
addedArray
=
[],
deletedArray
=
[];
forEach
(
changes
,
function
(
change
)
{
switch
(
change
.
status
)
{
case
'
added
'
:
addedArray
.
push
(
change
.
value
);
break
;
case
'
deleted
'
:
deletedArray
.
push
(
change
.
value
);
break
;
}
init
:
function
(
element
,
valueAccessor
,
allBindings
,
viewModel
,
bindingContext
)
{
var
$element
=
$
(
element
);
var
config
=
ko
.
toJS
(
valueAccessor
());
$element
.
multiselect
(
config
);
if
(
allBindings
.
has
(
'
options
'
))
{
var
options
=
allBindings
.
get
(
'
options
'
);
if
(
ko
.
isObservable
(
options
))
{
ko
.
computed
({
read
:
function
()
{
options
();
setTimeout
(
function
()
{
var
ms
=
$element
.
data
(
'
multiselect
'
);
if
(
ms
)
ms
.
updateOriginalOptions
();
//Not sure how beneficial this is.
$element
.
multiselect
(
'
rebuild
'
);
},
1
);
},
disposeWhenNodeIsRemoved
:
element
});
if
(
addedArray
.
length
>
0
)
{
$
(
element
).
multiselect
(
'
select
'
,
addedArray
);
}
if
(
deletedArray
.
length
>
0
)
{
$
(
element
).
multiselect
(
'
deselect
'
,
deletedArray
);
}
},
null
,
"
arrayChange
"
);
}
}
},
update
:
function
(
element
,
valueAccessor
,
allBindingsAccessor
,
viewModel
,
bindingContext
)
{
var
listOfItems
=
allBindingsAccessor
().
options
,
ms
=
$
(
element
).
data
(
'
multiselect
'
),
config
=
ko
.
utils
.
unwrapObservable
(
valueAccessor
());
if
(
isObservableArray
(
listOfItems
))
{
// Subscribe to the options: ko.observableArray incase it changes later
listOfItems
.
subscribe
(
function
(
theArray
)
{
$
(
element
).
multiselect
(
'
rebuild
'
);
});
//value and selectedOptions are two-way, so these will be triggered even by our own actions.
//It needs some way to tell if they are triggered because of us or because of outside change.
//It doesn't loop but it's a waste of processing.
if
(
allBindings
.
has
(
'
value
'
))
{
var
value
=
allBindings
.
get
(
'
value
'
);
if
(
ko
.
isObservable
(
value
))
{
ko
.
computed
({
read
:
function
()
{
value
();
setTimeout
(
function
()
{
$element
.
multiselect
(
'
refresh
'
);
},
1
);
},
disposeWhenNodeIsRemoved
:
element
}).
extend
({
rateLimit
:
100
,
notifyWhenChangesStop
:
true
});
}
}
if
(
!
ms
)
{
$
(
element
).
multiselect
(
config
);
}
else
{
ms
.
updateOriginalOptions
();
//Switched from arrayChange subscription to general subscription using 'refresh'.
//Not sure performance is any better using 'select' and 'deselect'.
if
(
allBindings
.
has
(
'
selectedOptions
'
))
{
var
selectedOptions
=
allBindings
.
get
(
'
selectedOptions
'
);
if
(
ko
.
isObservable
(
selectedOptions
))
{
ko
.
computed
({
read
:
function
()
{
selectedOptions
();
setTimeout
(
function
()
{
$element
.
multiselect
(
'
refresh
'
);
},
1
);
},
disposeWhenNodeIsRemoved
:
element
}).
extend
({
rateLimit
:
100
,
notifyWhenChangesStop
:
true
});
}
}
ko
.
utils
.
domNodeDisposal
.
addDisposeCallback
(
element
,
function
()
{
$element
.
multiselect
(
'
destroy
'
);
});
},
update
:
function
(
element
,
valueAccessor
,
allBindings
,
viewModel
,
bindingContext
)
{
var
$element
=
$
(
element
);
var
config
=
ko
.
toJS
(
valueAccessor
());
$element
.
multiselect
(
'
setOptions
'
,
config
);
$element
.
multiselect
(
'
rebuild
'
);
}
};
}
function
isObservableArray
(
obj
)
{
return
ko
.
isObservable
(
obj
)
&&
!
(
obj
.
destroyAll
===
undefined
);
}
function
forEach
(
array
,
callback
)
{
for
(
var
index
=
0
;
index
<
array
.
length
;
++
index
)
{
callback
(
array
[
index
]);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment