This is a migrated thread and some comments may be shown as answers.

[Solved] OnDocumentReady is firing twice

1 Answer 59 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dmitriy
Top achievements
Rank 1
Dmitriy asked on 21 Oct 2010, 04:19 PM
Hello there!
I'm new to Telerik MVC controls, so excuse me if this is something simple.

I started out with making a new TelerikMvcApplication solution.

Site.Master:
    </div>
    <%: Html.Telerik().ScriptRegistrar().DefaultGroup(group =>
          group.Combined(true).Compress(true).Add("Index.js")) %>
</body>
</html>

Index.aspx:
    <button>Testing</button>
    <%: Html.Telerik().ScriptRegistrar().OnDocumentReady("Index.Init()") %>
</asp:Content>

Index.js:
var Index = function () {
    return {
        Init: function () {
            alert('1');
        }
    };
} ();

When I load Index.aspx, the Init() function in Index.js is fired twice, so I see 2 alerts one after another.  If I assign a click handler to a button inside the Init() function and hit the button with FireBug, it is actually showing 2 events being assigned to the button.  Why is this happening?

Thank you!

EDIT:  I made some errors while copy/pasting my code here.

EDIT:  Ok, so I firebuged more.  Apparently, both calls to ScriptRegistrar are being rendered.  How would I make it render only the one in Master?

1 Answer, 1 is accepted

Sort by
0
Dmitriy
Top achievements
Rank 1
answered on 22 Oct 2010, 06:04 PM
To answer my own question, the problem was in the use of
<%:  %>
with colon.

To fix it, I removed colon character from both Master and View, and called Render() in Master.
Master:
<% Html.Telerik().ScriptRegistrar().DefaultGroup(group =>
   group.Combined(true).Compress(true).Add("json2.js")).Render(); %>

View:
<% Html.Telerik().ScriptRegistrar().Scripts(scripts =>
   scripts.AddGroup("IndexGroup", group =>
   group.Add("~/Scripts/Index.js"))).OnDocumentReady("Index.Init()");  %>
Tags
General Discussions
Asked by
Dmitriy
Top achievements
Rank 1
Answers by
Dmitriy
Top achievements
Rank 1
Share this question
or