来自ajax调用的内容的dojo

时间:2015-12-03 19:39:03

标签: javascript ajax dojo

案例: 我使用dojo来请求页面并将其加载到div(视图)中。

问题: 加载到表单中的内容包含dojo表单和相关对象,文本框等...如何控制这些小部件?我相信我正在解决这个问题的当前方式是草率的,可以更加精炼。

评论在代码中以帮助解释我的问题。请让我知道你的想法。

function (parser, domAttr, util, ready, dom, on, request, domStyle, registry, TextBox) {
//This prepares the doc main html page We are looking for a click of a menu option to load in thats pages pages content     

        ready(function () {     
            //Look for click of menu option            
            on(dom.byId('steps'), "a:click", function(e) {
                event.preventDefault();

                //Get the div we are going to load the page into
                var view = domAttr.get(this, "data-view");                
                // function that loads the page contents
                load_page(view);  
            });
        });    

        function load_page(view) {
            //First I see if this page already has widgets and destroy them
            //We do this so users can toggle between menu items
            // If we do not we get id already registered
            var widgets = dojo.query("[widgetId]", dom.byId('apply-view')).map(dijit.byNode);
            dojo.forEach(widgets, function(w){ 
                w.destroyRecursive();
            });


            //get the html page we are going to user for the menu item
            request.post("/apply_steps/"+view, {
                data: {
                    id: 2
                }
            }).then(
            function(response){
                //Add the content and parse the page   
                var parentNode = dom.byId('apply-view');
                parentNode.innerHTML = response;
                parser.parse(parentNode);

                //This is where it is sloppy
                //What I would prefer is to load a new js file the controlls the content that was just loaded
                //What happens now is I create a traffic director to tell the code what main function to use
                controller_director(view);
            },
            function(error){
                util.myAlert(0, 'Page not found', 'system-alert');
            });
        }   

        function controller_director(view) {
            //based on the view switch the function
            switch(view) {
                case 'screening_questions':
                    screening_questions();
                    break;
            }

        }  

        function screening_questions() {
            //Now we are controlling the page and its widgets
           // How would I get this info into a seperate js file that i would load along with the ajax call??
            ready(function () {         

                on(dom.byId('loginForm'), "submit", function(e) {
                    event.preventDefault();
                    var formLogin = registry.byId('loginForm');
                    authenticate();                   
                });
            }); 

            this.authenticate = function() {
                var formLogin = registry.byId('loginForm');
                if (formLogin.validate()) return;
            } 
        }    
});

0 个答案:

没有答案