0

在我的 bb 路由器中,我得到了一些 JSON 数据

$.ajax({
            url: "js/projects.json",
            success: function(projects) {

                database.projects = projects;

                var variables = {};
                var niz = new Array();
                var param = "Client"

                        $.each(projects, function()
                        {
                            if (!variables[this[param]])
                                variables[this[param]] = [];    
                            variables[this[param]].push(this);
                        });

                        for(var d in variables) {

                                niz.push(d);  

                                var number_of_clients = niz.length;
                        }

                        console.log(number_of_clients); 

                Backbone.history.start();
            }
        });

现在我想从我的一个视图中访问这个“项目”并使用该数据访问一些东西,我知道我可以像这里一样再次做同样的事情,但是它的重复代码,有没有办法在视图中获取这些数据?我尝试了几件事但没有成功,我总是得到未定义的

4

2 回答 2

0

You can attach your data to the window (window.projects = projects), then you can access it from everywhere. But I recomend you to use the ajax tools from Backbone, it's much easier and less messy.

于 2013-08-08T14:12:11.607 回答
0

Honestly, your question seems very suspicious design-wise. :)

Ajax calls should be made in models using the fetch function, not in a router, and not using jquery directly.

So I would advise you to redesign your app unless you have a strong reason not to do so.

于 2013-08-08T14:13:06.287 回答