Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backbone.js application wide functions that are not tied to a specific controller/model

I am using the backbone-boilerplate, which you can find here.

I was wondering how to add global functionality, which isn't explicitly tied to any specific collection, model, view, etc. An example would be a 'logout' function, which might look like this:

var logout = function(){
  // Clear Favorites
  // Handle asynchronous logging (all in-app logs are sent to the server at logout)
  // Redirect to the login page
  // Do other cleanup
}

Basically, this will handle numerous models/collections, including Favorites, Events, Logs, Users, and the application Router

If you look at the main.js file in the backbone-boilerplate, I have been adding these functions at the top (line 13) like this:

function(namespace, $, Backbone, Example){

  // BEGIN MY APP LOGIC
  namespace.app.logout = function(){
    // Do logout here
  };
  // END MY APP LOGIC

  var Router = Backbone.Router.extend({

This works fine, but the application logic can quickly grow out of control. My question is, what would be a better way to organize this code? If I had a Utils module and loaded that Utils module in, would it make more sense?

Cheers!

like image 226
andrewpthorp Avatar asked Apr 04 '12 17:04

andrewpthorp


People also ask

What is controller in Backbone JS?

These controllers are home made, just javascript objects with methods on them. They take the request from the router, collect the right data (collections, models...) and take the necessary view, combine them and pass the data into the view. from there on it's backbone again.

What is backbone JS model?

Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc. 1. It extends Backbone.

What is backbone JS give its features?

Backbone. js allows developers to develop one page applications and front-end much easier and better using JavaScript functions. Backbone provides different types of building blocks like models, views, events, routers and collections for assembling client side web applications.

Does anyone use Backbone JS?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.


1 Answers

TLDR: http://addyosmani.github.com/backbone-fundamentals/ Advanced section.

Make it possible, then make it beautiful, then make it fast.

As you grow your app and make things work (possible), it will become apparent which functions belong together. Separate them into modules accordingly (beautiful).

I use require.js, but not backbone-boilerplate (bb has an AMD branch as well). The way I have organized my app is exactly as you describe, I have a utils module that has general purpose app stuff. I then add Auth, Notification, and Date formatting stuff to the utils module. I then include the utils module wherever I'm lazy and want all my util functions available.

Since they are all just modules/mixins, I can also include the auth/notification/date modules in any single module I need them in.

like image 60
kmiyashiro Avatar answered Sep 27 '22 18:09

kmiyashiro