Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6368051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:37:36+00:00 2026-05-25T00:37:36+00:00

I have the following backbone.js code and i have a problem in that event

  • 0

I have the following backbone.js code and i have a problem in that event before i fetch the “add” event is triggered from the collections. Adding this.field.add(list_fields); in the success: fetch has resulted in an error. How do i make sure the model is fetched then the add event is run after that

$(function() {
        $(".chzn-select").chosen();
        /*********************************Models*************************************************/
        var Table = Backbone.Model.extend({

            urlRoot : '/campusfeed/index.php/welcome/generate'
        });
        var Field = Backbone.Model.extend({
            urlRoot: '/campusfeed/index.php/welcome/generate' 
        });
        /**************************Collections*************************************************/    
        Tables = Backbone.Collection.extend({
            //This is our Friends collection and holds our Friend models
            initialize : function(models, options) {
                this.bind("add", options.view.addFriendLi);
            //Listen for new additions to the collection and call a view function if so
            }
        });

        var Fields = Backbone.Collection.extend({
            model:Field,
            url:'http://localhost/campusfeed/index.php/welcome/generateFields',
            initialize : function(models, options) {
                this.bind("add", options.view.getFields);
            }
        });
        /************************************Views**************************************************/
        var m="shit";
        var app = Backbone.View.extend({
            el:'body',
            initialize:function(model,options){
                //Create collections in here

                this.table = new Tables(null,{
                    view : this
                });
                this.field = new Fields(null,{
                    view : this
                });
            },
            events : {
                "click #choose" : "generate"

            },
            generate:function(){
                var table = ( this.$("#table option:selected").text());
                var dbname = ( this.$("#database").text());
                var list_fields = new Field();
                list_fields.urlRoot = list_fields.urlRoot+"/"+dbname+"/"+table;
                list_fields.fetch({
                    success:function(){
                        console.log(JSON.stringify(list_fields));


                    }
                });

                this.field.add(list_fields);

            },

            getFields:function(model){

               console.log(JSON.stringify(model));


            }



        });
        var apprun = new app;
    /* var data = new Fields();
        data.url=data.url+"/some/data";
        alert(data.url);
        data.fetch();
        var staff = new Table();
        staff.fetch();
        var field = new Field();*/
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:37:37+00:00Added an answer on May 25, 2026 at 12:37 am

    the problem is the context of “this”. the success callback function has “this” set to the list_fields. you can work around this with a “self” or “that” variable:

            generate:function(){
                var table = ( this.$("#table option:selected").text());
                var dbname = ( this.$("#database").text());
                var list_fields = new Field();
                list_fields.urlRoot = list_fields.urlRoot+"/"+dbname+"/"+table;
                var that = this;
                list_fields.fetch({
                    success:function(){
                        console.log(JSON.stringify(list_fields));
    
                        that.field.add(list_fields);
                    }
                });
            },
    

    as a side note – your collections should never have a reference to a view. instead, your view should reference the collection and bind to the collection event

        var Fields = Backbone.Collection.extend({
            model:Field,
            url:'http://localhost/campusfeed/index.php/welcome/generateFields',
        });
    
        var app = Backbone.View.extend({
            initialize:function(model,options){
                //Create collections in here
    
                this.field = new Fields();
                this.field.bind("add", this.getFields, this);
            },
            getFields: function(){ ... }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using underscore.js's templating capabilities from backbone.js, I have the following template that
Lets have the following code : ( function($) { TeacherModel = Backbone.Model.extend({ defaults :
I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //
I have the following class that extends Backbone.View, I want all my backbone views
I have a backbone app with a view structure that looks like the following
I have the following Parent Object: Context = { ContextModel: Backbone.Model.extend({ //model Code }),
I have following code for an autocomplete box, I'm adding an image for choice
I have a working coffeescript/backbone idiom that looks like this: SidebarWidgets = ((() ->
I have the following HTML code: <ul id='item-list'></ul> <button id='add-item'>Add Item</button> <script type='text/template' id='item-template'>
I have the following collection: UsersMod.users_list = Backbone.Collection.extend({ add : function(model) { var duplicate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.