Nicolas
Nicolas

Reputation: 446

Insert script tag in Durandal

I am a bit struggling with what should be an easy task. I want to insert a script tag inside a div tag. The problem is that Durandal (for a reason that I still need to understand) seems to ignore those tags. This problem has been described here, but the solution proposed there does not work for me. how to stop durandal / jquery stripping out script tags from views?

Isn't there a simple way to tell Durandal to please interpret my script tags? This is becoming very frustrating, and besides the above question, I couldn't find anything on the web about this.

Thanks

Nicolas

Upvotes: 3

Views: 755

Answers (1)

Nicolas
Nicolas

Reputation: 446

Ok, here's what I do to get this to work. Inside the parent tag, I use a compose binding like so:

<td>
     <!-- ko compose: { view: 'views/homearea', model: 'viewmodels/homearea' } -->
     <!-- /ko-->
</td>

Inside my view, I have something like this:

<div id="homearea" style="height: 400px; width: 400px;">    
</div>

And inside the model, in the viewAttached method, I can put my custom javascript.

define(function () {

    var viewAttached = function () {
        $('#homearea').myFunction();
    };

    var vm = {
        viewAttached: viewAttached
    };
    return vm;
});

Upvotes: 1

Related Questions