I have a widget that allow me to pass a data-part. Based on if I informed a data-part to a instance of that widget, I want to show/hide something from the widget. So it is possible to detect on my widget viewmodel if the was specified something for a data-part?
Asked
Active
Viewed 306 times
2 Answers
0
The activate
method of a widget's view model gets passed a settings
object (this code is from the Durandal sample application):
ctor.prototype.activate = function(settings) {
this.settings = settings;
};
Within the settings
object, you can inspect the parent
property which will give you the node that the widget was bound to:
<div data-bind="expander:{items:projects}"></div>
That should give you access to the data-part
attribute on the node.

gerrod
- 6,119
- 6
- 33
- 45
0
You could use the composition
module's getParts
method within the widget's attached
event callback. See http://durandaljs.com/documentation/api#module/composition/method/getParts
// MyWidget viewmodel
define(['durandal/composition'], function (composition) {
var widget = function() {
var plugin = this;
plugin.showSomething = function () {
something.show();
);
// See http://durandaljs.com/documentation/Interacting-with-the-DOM
plugin.attached = function (view, parent) {
// See http://durandaljs.com/documentation/api#module/composition/method/getParts
var dataparts = composition.getParts(view);
plugin.parts = {
$partOne: $(dataparts.datapartnameone),
$partTwo: $(dataparts.datapartnametwo)
};
if (plugin.parts.$partOne) {
// An element with data-part="datapartnameone" was found in the host container
plugin.showSomething();
}
};
};
return widget;
});
// Example host container
<div data-bind="MyWidget: {}">
<span data-part="datapartnameone">My data-part</span>
</div>

Steven King
- 562
- 1
- 8
- 13