Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I put view related constants - backbone.js

Imagine that I have a view, and as part of a view it render 'x' model objects, and 'x' only. The question, where is it appropriate for me to put this view related constant?

My guess would be to do something like this:

myApp.MyView = Backbone.View.extend({
    ...
    myConstant: 10,
    ...
    render: function(){
        ...
        //some code that uses myConstant
        ...
    }
});

Does this make sense?

Any suggestions help!

like image 538
idbentley Avatar asked May 26 '11 17:05

idbentley


People also ask

What is a backbone view?

The Backbone. js Views specify how your data looks like. They represent model's data to the users. They can be used with any JavaScript template library. They handle users input events, bind events and methods, render model and collection and interact with users.

Should I use Backbone JS?

The developers should make use of Backbone JS while developing a single-page Java application. Backbone JS features Model View Framework, which allows much more than structuring JavaScript architecture. It will help the developers eliminate several issues that they might be facing while developing apps.

What is backbone JS used for?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.


1 Answers

It sounds like what you want to do is to assign a class property to the view. You can pass a second hash into your extend call to do this. Your code would look something like this:

myApp.MyView = Backbone.View.extend({

    render: function() {
        alert(myApp.MyView.myConstant);
    }

}, {

    myConstant: 10

});

where your constant is accessible as myApp.MyView.myConstant.

like image 157
Bill Eisenhauer Avatar answered Oct 23 '22 12:10

Bill Eisenhauer