0

I try to organize some views like this:

define([
  // Application.
  "app"
],

function(app) {

  var Partials = app.module();

  Partials.classes = {
    'AccountNavStart' : AccountNavStart = Backbone.View.extend({
       template: 'member/account-nav',
       serialize: function(){
         return { model: this.model };
       }
     });

  // Required, return the module for AMD compliance.
  return Partials;
});

Render:

new Popover.Views.Default({ 
    content: new Partials.classes['AccountNavStart']().render().$el
});

However I get an error:

Uncaught TypeError: Cannot call method 'get' of undefined

enter image description here

Any ideas how I get this error? How to fix it?

1 Answer 1

3

Try this:

EDIT: It's a Backbone Boilerplate app so I update the answer:

The problem is that how to get a view's $el of "Partials" module after it being rendered. I try to define a module and declare a Backbone view as its member. Then I create a instance of view then chain the method call via view.render().$el. It get the view's $el successfully because in Backbone Boilerplate render() has its default behavior. Native backbone render function is designed to be overridden.

My simple example:

Module declaration (modules/user):

define(["app"],

function(app){
    var User = app.module();

    User.Views.Item = Backbone.View.extend({
        template: "user/item",
        tagName: "li",
        serialize: function(){
            return {model: this.model};
        },
        initialize: function(){
            this.listenTo(this.model, "change", this.render);
        }
    });

return User;

});

Item template (templates/user/item.html)

<a href="#"><%=model.get("name")%></a>

Try to get view's $el in Router initialize call

define([
// Application.
"app","modules/user"
],

function(app, User) {

    // Defining the application router, you can attach sub routers here.
    var Router = Backbone.Router.extend({
    initialize: function(){
        app.useLayout("main-layout").setViews({
            ".users":new User.Views.Item({model:new Backbone.Model({"name":"cccc"})})
        }).render();

        //try to access $el. it works
        console.log(new User.Views.Item({model:new Backbone.Model({"name":"cccc"})}).render().$el);
    },
    routes: {
        "": "index",
        "user/:name":"user"
    },

    index: function() {

    },
    user: function(){

    }
    });

    return Router;

});

Finally I got the $el:

$el!

Hope this is helpful to you.

Sign up to request clarification or add additional context in comments.

11 Comments

Good point : backbonejs.org/#View-render "A good convention is to return this at the end of render to enable chained calls."
Ok the error is gone, however now it does not render the template but just a empty div. Any ideas why? Render is triggered but it does not render the template
I have modified the answer the render function will render the view template from model data.
I get an error Uncaught TypeError: Cannot read property 'attributes' of undefined
Answer updated. You have to assign the model when you initiate the backbone view.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.