Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干网和Laravel-选择团队并为该团队生成用户_Javascript_Php_Backbone.js_Laravel - Fatal编程技术网

Javascript 主干网和Laravel-选择团队并为该团队生成用户

Javascript 主干网和Laravel-选择团队并为该团队生成用户,javascript,php,backbone.js,laravel,Javascript,Php,Backbone.js,Laravel,我是backbone.js的新手,我学习了Jeffery Way关于Laravel和backbone的教程。我目前有一个显示的团队列表,并从数据库中附加了他们的ID。我设置了一个事件,所以当我点击一个团队时,它会向laravel发送一个get请求,并返回所有拥有该团队id的用户 这个请求非常有效,只是我一直在尝试生成一个usersview。除了id之外,我无法从App.Views.Users中的集合中提取任何数据 我这样做对吗 View.js //Global App View App.View

我是backbone.js的新手,我学习了Jeffery Way关于Laravel和backbone的教程。我目前有一个显示的团队列表,并从数据库中附加了他们的ID。我设置了一个事件,所以当我点击一个团队时,它会向laravel发送一个get请求,并返回所有拥有该团队id的用户

这个请求非常有效,只是我一直在尝试生成一个usersview。除了id之外,我无法从App.Views.Users中的集合中提取任何数据

我这样做对吗

View.js

//Global App View
App.Views.App = Backbone.View.extend({
    initialize: function() {
            var allTeamsView = new App.Views.Teams({ collection: App.teams }).render();
    }
});

App.Views.Teams = Backbone.View.extend({

    tagName: 'div',

    events: {
            "click a" : "teamClicked"
    },
    teamClicked: function(e){
            e.preventDefault();
            var team_id = e.target.id;
            teamusers = new App.Models.Team({id: team_id});
            collection = new App.Collections.Teams([teamusers]);
            teamusers.fetch();
            view = new App.Views.Users({collection: collection, el: '#usersList'});
            view.render();
            return false;
    }, 
attributes: function() {
            return{
                    class: 'span2 admin teams',
                    id: 'inner-content-div'
            };
    },

    render: function(){
            this.collection.each( this.addOne, this );
            return this;
    },

    addOne: function(team){
            var teamView = new App.Views.Team({model: team});
            this.$el.append(teamView.render().el);
    }
});


// Single Team View
App.Views.Team = Backbone.View.extend({
    tagName: 'li',

    template: template('allTeamsTemplate'),

    attributes: function() {
            return{
                    id: this.model.get('id')
            };
    },

    render: function(){
            this.$el.html( this.template( this.model.toJSON() ));
            return this;
    }

});

App.Views.Users = Backbone.View.extend({
    tagName: 'ul',

    render: function(){
            this.collection.each( this.addOne, this );
            console.log(collection);
            return this;
    },

    addOne: function(user){
            var userView = new App.Views.User({model: user });
            this.$el.append(userView.render().el);
    }
});


// Single User View
App.Views.User = Backbone.View.extend({
    tagName: 'li',

    template: template('allUsersTemplate'),

    attributes: function() {
            return{
                    id: this.model.get('id')
            };
    },

    render: function(){
            this.$el.html( this.template( this.model.toJSON() ));
            return this;
    }

});
    teamsUserList: function(e){
        e.preventDefault();
        var team_id = e.target.id;
        var collection = new App.Collections.TeamsUsers([], {id: team_id});
        collection.fetch().complete(function() {
            var view= new App.Views.Users({collection: collection}); 
            $('#usersList').append(view.render().el);
});

}, 
TeamController.php

public function show($id)
{
  return Team::find($id)->user;
}
Index.Blade.php

        <div class="teamContainer" id="demo"></div>
        <ul class="userContainer" id="usersList"></ul>
            <script type="text/template" id="allUsersTemplate">
                <p><%= id %></p>
            </script>
            <script type="text/template" id="allTeamsTemplate">
                <a id='<%= teamNum %>' ><%= teamName %></a>
            </script>    

我让它工作了。问题在于我的活动

View.js

//Global App View
App.Views.App = Backbone.View.extend({
    initialize: function() {
            var allTeamsView = new App.Views.Teams({ collection: App.teams }).render();
    }
});

App.Views.Teams = Backbone.View.extend({

    tagName: 'div',

    events: {
            "click a" : "teamClicked"
    },
    teamClicked: function(e){
            e.preventDefault();
            var team_id = e.target.id;
            teamusers = new App.Models.Team({id: team_id});
            collection = new App.Collections.Teams([teamusers]);
            teamusers.fetch();
            view = new App.Views.Users({collection: collection, el: '#usersList'});
            view.render();
            return false;
    }, 
attributes: function() {
            return{
                    class: 'span2 admin teams',
                    id: 'inner-content-div'
            };
    },

    render: function(){
            this.collection.each( this.addOne, this );
            return this;
    },

    addOne: function(team){
            var teamView = new App.Views.Team({model: team});
            this.$el.append(teamView.render().el);
    }
});


// Single Team View
App.Views.Team = Backbone.View.extend({
    tagName: 'li',

    template: template('allTeamsTemplate'),

    attributes: function() {
            return{
                    id: this.model.get('id')
            };
    },

    render: function(){
            this.$el.html( this.template( this.model.toJSON() ));
            return this;
    }

});

App.Views.Users = Backbone.View.extend({
    tagName: 'ul',

    render: function(){
            this.collection.each( this.addOne, this );
            console.log(collection);
            return this;
    },

    addOne: function(user){
            var userView = new App.Views.User({model: user });
            this.$el.append(userView.render().el);
    }
});


// Single User View
App.Views.User = Backbone.View.extend({
    tagName: 'li',

    template: template('allUsersTemplate'),

    attributes: function() {
            return{
                    id: this.model.get('id')
            };
    },

    render: function(){
            this.$el.html( this.template( this.model.toJSON() ));
            return this;
    }

});
    teamsUserList: function(e){
        e.preventDefault();
        var team_id = e.target.id;
        var collection = new App.Collections.TeamsUsers([], {id: team_id});
        collection.fetch().complete(function() {
            var view= new App.Views.Users({collection: collection}); 
            $('#usersList').append(view.render().el);
});

}, 

JavaScript中缺少很多变量。teamusers.fetch;是一个AJAX调用,因此在服务器响应之前,模型中不会有任何有用的内容。您向用户发送集合的部分在您提供的代码中不可用,请确保您没有缺少集合/模型的名称空间。.etc在事件teamClicked下的App.Views.Teams视图下。我以为我初始化了一个新的用户视图来显示我刚刚获取的集合?