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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:38:26+00:00 2026-06-18T21:38:26+00:00

I am developing a backbone application which is using require.js. I want a user

  • 0

I am developing a backbone application which is using require.js.

I want a user to enter in the ‘id’ for a model and then either be redirected to a view for that model if it exists, or display an error message if it does not. This sounds extremely simple, but I am having trouble figuring out the roles of each component.

In the application below, the user will come to an index page with an input (with id ‘modelId’) and a button (with class attribute ‘lookup’).

The following piece of code is the router.

define(['views/index', 'views/myModelView', 'models/myModel'], 
    function(IndexView, MyModelView, myModel) {
    var MyRouter = Backbone.Router.extend({
        currentView: null,

        routes: {
            "index": "index",
            "view/:id": "view"
        },

        changeView: function(view) {
            if(null != this.currentView) {
                this.currentView.undelegateEvents();
            }
            this.currentView = view;
            this.currentView.render();
        },

        index: function() {
            this.changeView(new IndexView());
        },

        view: function(id) {
            //OBTAIN MODEL HERE?
            //var model
            roter.changeView(new MyModelView(model))
        }

    });

    return new MyRouter();
});

The following piece of code is the index view

define(['text!templates/index.html', 'models/myModel'], 
    function( indexTemplate, MyModel) {
    var indexView = Backbone.View.extend({
        el: $('#content'),

        events: {
            "click .lookup": "lookup"
        },

        render: function() {
            this.$el.html(indexTemplate);
            $("#error").hide();
        },

        lookup: function(){
            var modelId = $("#modelId").val()
            var model = new MyModel({id:modelId});
            model.fetch({
                success: function(){
                    window.location.hash = 'view/'+model.id;
                },
                error: function(){
                    $("#error").text('Cannot view model');
                    $("#error").slideDown();
                }
            });
        },
    });
    return indexView
});

What I can’t figure out is that it seems like the better option is for the index view to look up the model (so it can display an error message if the user asks for a model that doesn’t exist, and also to keep the router cleaner). But the problem is that the router now has no reference to the model when the view/:id router is triggered. How is it supposed to get a hold of the model in the view() function?

I guess it could do another fetch, but that seems redundant and wrong. Or maybe there is supposed to be some global object that both the router and the view share (that the index view could put the model in), but that seems like tight coupling.

  • 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-18T21:38:27+00:00Added an answer on June 18, 2026 at 9:38 pm

    You can do something like this. You could do something similar with a collection instead of a model, but it seems like you don’t want to fetch/show the whole collection?

    With this type of solution (I think similar to what @mpm was suggesting), your app will handle browser refreshes, back/forward navigation properly. You basically have a MainView, which really acts more like a app controller. It handles events triggered either by the router, or by user interaction (clicking lookup or a back-to-index button on the item view).

    Credit to Derick Bailey for a lot of these ideas.

    In the Router. These are now only triggered if the user navigates by changing a URL or back/forward.

        index: function() {
            Backbone.trigger('show-lookup-view');
        },
    
        view: function(id) {
            var model = new MyModel({id: id});
            model.fetch({
                success: function(){
                    Backbone.trigger('show-item-view', model);
                },
                error: function(){
                    // user could have typed in an invalid URL, do something here,
                    // or just make the ItemView handle an invalid model and show that view...
                }
            });
        }
    

    In new MainView, which you would create on app startup, not in router:

    el: 'body',
    
    initialize: function (options) {
        this.router = options.router;
    
        // listen for events, either from the router or some view.
        this.listenTo(Backbone, 'show-lookup-view', this.showLookup);
        this.listenTo(Backbone, 'show-item-view', this.showItem);
    },
    
    changeView: function(view) {
        if(null != this.currentView) {
           // remove() instead of undelegateEvents() here
           this.currentView.remove();
        }
        this.currentView = view;
        this.$el.html(view.render().el);
    },
    
    showLookup: function(){
        var view = new IndexView();
        this.changeView(view);
        // note this does not trigger the route, only changes hash.
        // this ensures your URL is right, and if it was already #index because
        // this was triggered by the router, it has no effect.
        this.router.navigate('index'); 
    },
    
    showItem: function(model){
        var view = new ItemView({model: model});
        this.changeView(view);
        this.router.navigate('items/' + model.id); 
    }
    

    Then in IndexView, you trigger the ‘show-item-view’ event with the already fetched model.

        lookup: function(){
            var modelId = $("#modelId").val()
            var model = new MyModel({id:modelId});
            model.fetch({
                success: function(){
                    Backbone.trigger('show-item-view', model);
                },
                error: function(){
                    $("#error").text('Cannot view model');
                    $("#error").slideDown();
                }
            });
        },
    

    I don’t think this is exactly perfect, but I hope it could point you in a good direction.

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

Sidebar

Related Questions

I'm developing application using backbone.js & jquery. I have following code in model: runReport:
I am developing a Backbone web application and I want to know that how
I am starting developing an application with backbone+require. I want to share a same
A Backbone app which I'm developing has a collection and a model, and associated
Iam developing one application.In that iam placing the radio buttons(uiimageview) on table view and
Developing a web application that will act similar to a daily journal. The user
I am currently developing an application using Phonegap / Jekyll / Backbone. I am
I am developing a Backbone app which heavily relies on a REST API using
On a website I'm developing, using Node, Express, and Backbone, I have the user
I'm developing a mobile application using Backbone, jQueryMobile and Phonegap. The app works great

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.