0

i have several singleton views in my SPA, each of these view contain the same widget.

When the view is activated i take some parameters from the activate callback and pass it to the widget and it works fine.

But if i navigate the second time into the view (with different parameters into the activate callback) the activate method of the widgets is rightly not raised.

How can i pass the fresh data to the widgets ?

I tried to make the parameter observable and subscribe it into the widget (settings.params.subscribe) and it works, but i don't think it's a good solution.

  • 1
    A possible solution would be to change your views from returning singletons to returning a constructor function. Then, the activate method would be triggered every time you navigate to the view. – Brett May 16 '14 at 18:08

1 Answers1

2

This should be pretty simple assuming you are returning a constructor from your widget -

View model -

var thisWidget = new widget(someArbitraryData)

function createWidget() {
    dialog.show(thisWidget);
}

// later
function updateWidget() {
    thisWidget.refreshData(newArbitraryData);
}

Widget module -

define([], function () {
    var ctor = function () {
        var self = this;
        self.data = ko.observable();
    };
    ctor.prototype.refreshData = function (newData) {
        var self = this;
        self.data(newData);
    };
    ctor.prototype.activate = function (activationData) {
        var self = this;
        self.data(activationData);
    };
});
PW Kad
  • 14,953
  • 7
  • 49
  • 82
  • but the widget is referenced into the html markup, i have no references in the viewmodel of the page. – user2961498 May 20 '14 at 19:46
  • That doesn't make any sense so you would have to provide an example. – PW Kad May 20 '14 at 19:53
  • What I mean is : having my widget files (view.html and viewmodel.js) located in App/widgets/widgetName, in each view i can use the markup `
    ` and the viewLocator takes care about the binding between the view and viewmodel. If I want to instantiate the widget from the viewmodel i should require the widget module into the view, but how can bind the view to my object ? Using the compose binding i got an error, because it's looking for the view into the standard folder, not in the widgets one.
    – user2961498 Jun 04 '14 at 07:32
  • i have also read your interesting post [link](http://pwkad.wordpress.com/2014/02/02/best-parts-about-durandal/). I got the point. Correct me if I am wrong : you created the widget (not singleton module) but not using the durandal widget plugin convention, that's why the composition is working.+ – user2961498 Jun 04 '14 at 08:24
  • That's correct. The widget plugin is just a helper but I find that its not as verbose and scalable as just creating a widgets folder inside your views and viewmodels folders – PW Kad Jun 04 '14 at 11:54