Coder Social home page Coder Social logo

evereq / backbonejs-router-title-helper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pwhisenhunt/backbonejs-router-title-helper

1.0 2.0 0.0 184 KB

Overrides Backbone.js router to provide title updates based on the current route.

JavaScript 98.93% CSS 1.07%

backbonejs-router-title-helper's Introduction

Backbone.js Router Title Helper Build Status

When developing single page applications the page title is often over looked and the usability of your application suffers. This script aims to make it easier to update the page title of your application by providing a single place for maintaining your applications page titles. This script overrides the Backbone.js Router to provide page title updates based on the current route. Note: if you have another plugin that touches the Backbone.Router you may run into issues.

Use

This script is tested against Backbone.js 1.0.0. Include the script in your page after Backbone.js has loaded.

<script src="backbone.js" type="text/javascript"></script>
<script src="backbone.router.title.helper.js" type="text/javascript"></script>

and then in your router provide a titles object literal whose keys map to route function names and whose values are page titles. Be sure to provide a default for routes to fall back to in case you forget to include a title. An error is thrown if you fail to specify a routes title and a default title is not found. Example router:

var Router = Backbone.Router.extend({
    routes: {
        'a': 'aRoute',
        'b': 'bRoute'
    },

    titles: {
        'aRoute': 'aTitle',
        // this is the default title that routes will fall back to
        'default': 'defaultRoute'
    },

    aRoute: function() {
        // This will trigger the page title aRoute
    },

    bRoute: function() {   
        // This will trigger the page title defaultRoute since there is no bRoute specified
    }
});

You can also specify a function or a router method name as the return value for a title. This is useful when you need to compute a value for a title. For example:

var Router = Backbone.Router.extend({
    ...

    appName: "Sample Application",

    titles: {
        'cRoute': function() {
            return "cRouteTitle — " + this.appName;
        },
        'dRoute': "dRouteTitle"
    },

    dRouteTitle: function() {
        return "dRouteTitle — " + this.appName;
    }

    ...
});

Contribution

Feel free to contiribute!

First run npm install in the project directory to fetch dependencies. Then simply run grunt to run linter, tests and to build minified version.

backbonejs-router-title-helper's People

Contributors

evereq avatar pwhisenhunt avatar romanenko avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.