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 9237991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:39:21+00:00 2026-06-18T07:39:21+00:00

Scenario I am working on backbone app. What is happening right now is when

  • 0

Scenario

I am working on backbone app. What is happening right now is when user clicks edit link on page then it should show a form. I am trying to implement this using backbone routers rather than events. With events object it works perfectly fine. To use routers, I am using global events.

Problem

The problem is that when user clicks on edit link, it shows me following error in console

Uncaught TypeError: Object 10 has no method 'toJSON'             views.js:57

This error is because on line 57 in views.js, I am using this.model.toJSON() whereas I am not passing model via router. I don’t know how pass model through router

Here is my router. Note: All of the following codes are in separate files

App.Router = Backbone.Router.extend({
    routes: {
        'contacts/:id/edit': 'editContact'
    },

    editContact: function (id) {
        console.log('yahhhhh');
        vent.trigger('contact:edit', id);
    }
});

In above router I am triggering an event inside editContact function. Then I am listening to above event in following initialize function.

App.Views.App = Backbone.View.extend({
    initialize: function () {
        vent.on('contact:edit', this.editContact, this);
    },

    editContact: function (contact) {
        var editContactView = new App.Views.EditContact({ model: contact });
        $('#editContact').html(editContactView.render().el);
    }
});

Now in above after listening to event in initialize function, I am calling editContact function and I am also passing model using this keyword. Inside editContact function, I am creating an instance of EditContact, view which is following, and then rendering a form which needs to be shown.

App.Views.EditContact = Backbone.View.extend({
    template: template('editContactTemplate'),

    render: function () {
        var html = this.template(this.model.toJSON());  //<--- this is line 57
        this.$el.html(html);
        return this;
    }
});

After doing all of the above, the form is not shown and I am getting above mentioned error.

Question

How do I pass model to render function inside EditContact via router so that it starts working?

UPDATE

Here is my model

App.Models.Contact = Backbone.Model.extend({
    urlRoot : '/contacts'
});
  • 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-06-18T07:39:22+00:00Added an answer on June 18, 2026 at 7:39 am

    In your editContact method the argument contact refers to the id you pass onwards from the router. When you initialize a new view with new App.Views.EditContact({ model: contact }) the model of the view will, expectedly, be the id.

    You need to map the id into a model instance. IMHO the correct place to do this is in the router:

    editContact: function (id) {
        var contact = new App.Models.Contact({id:id});
        vent.trigger('contact:edit', contact);
    }
    

    Notice that at this point the model will only have the id property set. If you need to populate the model properties for editing, you should fetch the model from the server, and only then trigger the event:

    editContact: function (id) {
        var contact = new App.Models.Contact({id:id});
        contact.fetch().done(function() {
          vent.trigger('contact:edit', contact);
        });
    }
    

    Edit based on comments: Generally speaking you shouldn’t pass anything to the router. The router should be a starting point for every new request (url change). If you want to hold some state between page changes, you should store the data on the router level, and pass the models and collections “down” from the view.

    In a simplified scenario this would mean initializing and storing a reference to the collection in the router. Something like:

    var Router = Backbone.Router.extend({
      initialize: function() {
        this.contactCollection = new App.Collections.Contacts();
      },
    
      editContact: function (id) {
        id = parseInt(id, 10);
        if(_.isNaN(id)) {
          //error...
        }
        //try to get a model from the collection
        var contact = this.contactCollection.get(id);
    
        //if not found, create, add and fetch
        if(!contact) {
          contact = new App.Models.Contact({id:id});
          this.contactCollection.add(contact);
          contact.fetch().done(function() {
            vent.trigger('contact:edit', contact);
          });
        } else {
          vent.trigger('contact:edit', contact);
        }
      }
    });
    

    Please note that this is just example code, and not necessarily how you should implement it, line by line. You should consider whether it’s OK to display a potentially stale model in the view, or whether you should always fetch it from the server. In practice you might also abstract the collection state in a nice class, instead of handling it directly in the router.

    Hope this answers your questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a scenario that I am working on : Right now we have a
Scenario: I'm working on a rails app that will take data entry in the
the working scenario on a web bases app like a forum, the users can
I have multiple Google Maps on a single page. Scenario: Maps are working as
Working scenario: When I run my app from xcode directly to my device I
I'm working on a asp.net page and I have the following scenario: I have
Scenario: I am working on a Android project where in one particular openGL page,
Let's me show first the scenario that is working: I have a HandlerAttribute that
I'm working on a scenario in which the user would tab into an input
Scenario : I am working on LOB application, as in silverlight every call to

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.