A
A
Andrey Dyrkov2015-04-09 15:07:01
JavaScript
Andrey Dyrkov, 2015-04-09 15:07:01

How to encapsulate in backbone?

When creating models, views, collections, we create them in the global scope, is that bad?
How to encapsulate all this but at the same time so that different application modules have access to each other, can communicate?
For example, this is how I can hide all the code from the global scope

(function(Backbone,_){
  var User = Backbone.Model.extend({
    defaults:{
      name:'user',
      login:'почта не указана',
      pass: 'пароль не указан'
    },
    validate:function(attr){
      if(name.length < 3){
        return 'Имя содержит слишком мало символов!';
      }
    }
  });

var user = new User({name:'Vasia',login:'[email protected]',pass:'root'});

})(Backbone,_);

But at the same time, I do not have access to this block of code, only if the function will take some parameters.
You can throw off the code of a working small application, I can figure it out myself))

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin Kitmanov, 2015-04-09
@VIKINGVyksa

Discover a real modular system - Require.JS or Browserify . The first is more flexible, but does not require a compilation step, the second has more plugins and has less overhead.
PS For good, it is already possible (and even necessary) to start writing in ES2015 and use local modules and other goodies.

A
Alexey Ukolov, 2015-04-09
@alexey-m-ukolov

Use Marionette , it has a module system and many other useful things.

S
Sergey Melnikov, 2015-04-09
@mlnkv

var App = App || {};
App.User = Backbone.Model.extend({});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question