How to stop durandal / jquery by removing script tags from views?

I know there are better templates, but in this case there is some old code that we would like to move to the view. This html has

<script src="binding.js"></script> 

which should be launched after displaying and displaying the page.

I was hoping to do this with the <script src=""> tag, but the tag seems to be retracting.

How do we get the script tag for rendering or crawl with durandal?

Html example:

 <div> <link rel="stylesheet" href="/Content/stylesheet1.css"/> <link rel="stylesheet" href="/Content/stylesheet2.css"/> <header> <h1>heading</h1> <h2>sub heading</h2> </header> <section> </section> <footer> </footer> <script src="dobinding.js"></script> </div> 

This is useful:

 http://knockoutjs.com/documentation/custom-bindings.html 
+4
source share
3 answers

You can use the KnockOut custom binding for this.

Here's a really simple snap - keep in mind that this is just an idea:

 ko.bindingHandlers.script = { update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { var scriptName = ko.utils.unwrapObservable(valueAccessor()); $(element).html("<script src='" + scriptName + "'></script>"); } }; 

In your view (.html file):

 <div data-bind="script:'dobinding.js'"></div> 

I tested this, and I found that the script is actually being entered, parsed and executed, but it does not remain loaded in the DOM. This may or may not be a problem for you.

It seems that tagging may be the subject of Durendal. I still do not understand the reason, but I will update if I have the opportunity to look into it.

+4
source

I believe that the durandal presentation engine uses only the first html element in the view and ignores everything else. Therefore, if the script tag is outside the main container in your view, I think it will be removed.

Can you post some html / js for viewing?

+1
source

If this is not the case, you can wrap everything that happens inside the script in the function (let it call doBindings) and then paste the script into your index.html file (or equivalent), along with Knockout, JQuery, etc.

Then, in any view, you need this code to work, add the viewAttached method to the view model and make a doBindings call:

 activate: function() { ... } viewAttached: function() { doBindings(); } 

This will call the function when loading the view and access the DOM.

The solution is here: Insert the script tag in Durandal , but the answer here would save me an additional search, so I added it here too.

0
source

All Articles