S
S
semolex2015-04-09 18:32:07
JavaScript
semolex, 2015-04-09 18:32:07

How to correctly pass render parameters to Backbone?

Hello!
I have the following problem:
I decided to re-architect my Backbone test in the following way.
At first, my collections were fetched directly in the View, in the render function. Heard. that it’s not good to do this, so I’m redoing it so that the collection is fetched in the router, and the data is transferred to the view.
I tried doing this:
view:

define([
    'jquery',
    'underscore',
    'backbone',
    'form2js',
    "pages/test/collections/users",
    "pages/test/models/user",
    'text!pages/test/templates/users.html'
], function($, _, Backbone, form2js, UsersCollection, UserModel, UsersTemplate) {
    var UsersView = Backbone.View.extend({
        el: '#main_content',

        initialize: function(users) {
            this.users = users;
            _.bindAll(this, 'render', this);
        },
        render: function() {
            var that = this;
            var template = _.template(UsersTemplate, {
                users: users;
            });
            this.$el.html(template);
        }

And, actually routes.
define([
        "pages/test/views/usersView",
        "pages/test/collections/users",
    ],
    function(usersView,  UsersCollection) {
        return Backbone.Router.extend({
            routes: {
                "users": 'users',
            },
            users: function() {
                users = new UsersCollection();
                users.fetch();
                console.log(users);
                if (typeof UsersView === "undefined") {
                    UsersView = new usersView();
                };
                UsersView.render({
                    users: users
                });
            }

The problem is this:
Now nothing is rendered, and there UsersView = new usersView();is an error on the line in the console
routes.js: Uncaught TypeError: undefined is not a function
. I understand that I'm doing something wrong, but unfortunately I'm not very familiar with JS to solve the problem myself.
I will be grateful for advice and answers!
ps Is it correct to fetch a collection in router? If not, where should it be done?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2015-04-09
@semolex

Did you forget to return UsersView at the end of pages/test/views/usersView?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question