Code Monkey home page Code Monkey logo

marionette-resources's Introduction

marionette-resources

###What are resources?### Resources are named sets of route components.

resources = {
    "games": {
        root  : true
      , url   : function () { return "games"; }
      , path  : _.template("#games")
      , action: function () { /*...*/ }
    }
  , "game": {
      , url   : function () { return "games/:id"; }
      , path  : _.template("#games/<%= id %>")
      , action: function (id) { /*...*/ }
    }
};

###Why use resources?### Using resources decouples the route components from the application, controllers, views:

// Use Marionette.Resources Application API
App = new Marionette.Application();
App.redirectTo("game", {id: 1});
    
// ...instead of referring to a route directly
App = new Marionette.Application();
Backbone.history.navigate("games/1", {trigger: true});

...and templates:

<!-- Use Marionette.Resources Template Helpers API -->
<a href="<%= getPath('game', {id: 1}) %>">Show game 1</a>

<!-- ...instead of referring to a route directly... -->
<a href="#games/1">Show game 1</a>

###How do I use resources?### Add resources to a Marionette.AppRouter class or instance with the name "appResources".

// Add resources to an AppRouter class
Router = Marionette.AppRouter.extend({
  appResources: { /*...*/ }
});

// ...or instance
new Marionette.AppRouter({
  appResources: { /*...*/ }
});

Instantiating the Marionette.AppRouter will expose the appResources to the Resource APIs and automatically configure the routes. Each resource that contains both url and action methods will generate a route.

App    = new Marionette.Application();
router = new Marionette.AppRouter({
  appResources: {
    "edit_game": {
          url   : function () { return "games/:id/edit"; }
        , path  : _.template("#games/<%= id %>/edit")
        , action: function (id) { /* ... */ }
      }
  }
});

router.appRoutes  // => {"games/:id/edit": "edit_game"}
router.controller // => {"edit_game": function (id) { /* ... */ }}
App.getPath("edit_game", {id: 1}); // => "#games/1/edit"

###Can I create non-routable resources?### Yes. In the case where a resource does not need to be routable, define it with only an action method.

App = new Marionette.Application();
new Marionette.AppRouter({
  appResources: {
    "new_game": {
        action: function (region) { /* ... */ }
    }
  }
});

// Add a region to the app and render a "new_game" in it.
App.addRegions({"region": "#region"});
App.render("new_game", App.region);

marionette-resources's People

Contributors

chrisf74 avatar

Watchers

James Cloos avatar Maxim Denisov 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.