npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

marionette_lib

v4.0.18

Published

marionette_lib

Downloads

10

Readme

marionette_lib

An opinionated structure for building Marionette apps highly based on the excellent Backbone Rails Series.

This library is all about the UI so it doesn't alter your models and collections.

Configuration

You need to run the configure method to tell this library about your app. Here is an example of the configuration options.

marionette_lib = require 'marionette_lib'
marionette_lib.configure {
  app: myApp
  handlebars: require('handlebars')
}

Project Structure

This is the usual project structure for apps that use this library.

- /apps
  |- /section_name
     |- /edit
        |- Edit.coffee
        |- EditController.coffee
     |- /list
        |- List.coffee -- if needed
        |- item.hbs -- if needed
        |- list.hbs -- if needed
        |- ListController.coffee
     |- /show
        |- view.hbs -- template if needed
        |- View.coffee
        |- ShowController.coffee
     |- app.coffee
     |- index.styl
- /behaviors
- /components
- /config
- /entities
- app.coffee
- main.coffee
- main_config.coffee

Behaviors

Modifiers

This is a simple behavior baked in to all views and layouts which allows you work with BEM style modifiers. It will also trigger events on the view when modifiers are turned on and off.

class MyView extends marionette_lib.views.ItemView
  name: 'my'
  initialize: ->
    @on 'modified:shown', (state) ->
      console.log('are we in the shown state?', state.on)
  
  showSomething: ->
    @triggerMethod 'modified', 'shown'
    
  toggleSomething: ->
    @triggerMethod 'modified', 'shown', { toggle: true }
    
  removeSomething: ->
    @triggerMethod 'modified', 'shown', { remove: true }
  

I the example above you will get the my--shown class added, toggled and then removed from the view's element.

You will also get the modified:[modifier] event triggered on the view. Which you can subscribe to like:

@on 'modified:shown`, (state) ->
  console.log(state.on) # true/false

You can also use the Marionette method form:

onModifiedShown: (state) ->
  console.log(state.on) # true/false

Components

Config

Controllers

Base Controller

The base controller makes sure that all controllers register themselves in the app registry so we can keep track of what has or hasn't been cleaned up properly.

App Controller

The app controller is used for managing portions of the app like a listing or showing an item.

ListController

Here is a simple example of how you might write a ListController.coffee.

List = require './List'

class PeopleListController extends marionette_lib.controllers.App
  initialize: ->
    @people = new Person.Collection()
    @list = new List(collection: @people)
    @people.fetch()
    @show @list,
      loading: true

If you needed a layout with regions then the controller would manage what goes in to the regions.

Layout = require './Layout'
List = require './List'
Stats = require('components').Stats

class PeopleListController extends marionette_lib.controllers.App
  initialize: ->
    @people = new Person.Collection()
    @layout = new Layout()
    @list = new List(collection: @people)
    
    # an example component controller
    @peopleStats = new Stats(collection: @people)
    
    # once the layout is shown we want to show the sub views
    @layout.on 'show', =>
      @show @list, 
        region: @layout.listRegion
        loading: true
      @show @peopleStats, 
        region: @layout.statsRegion
        loading: true
    
    # fetch the data
    @people.fetch()
    
    # show the layout
    @show @layout

Component Controller

Router Controller

The router controller allows you to manage the routes along with controlling the access to them. Here is an example of how this might be used.

loggedInPolicy = new marionette_lib.controllers.Router.ActionPolicy({
  isAuthorized: (actionName, actionConfig) ->
    return userIsLoggedIn
})
router = new marionette_lib.controllers.Router
  defaultPolicy: loggedInPolicy
  actionUnauthorized: (actionName, actionConfig) ->
    doSomethingWhenUnauthorized()
  # these actions whill be available on the object
  # ie router.onlywhenLoggedIn()
  actions: {
    onlyWhenLoggedIn: (name, options) ->
      # this will only be called if loggedInPolicy
      # is authorized
    customPolicy: {
      fn: ->
        doSomething()
      policy: new marionette_lib.controllers.Router.ActionPolicy({
        isAuthorized: (actionName, actionConfig) ->
          return checkCustomState()
      })
      unauthorized: (actionName, actionConfig) ->
        # this will override the main actionUnauthorized method
        doSomethingWhenUnauthorized()
    }
  }

Static Controller

The Static Controller is useful to be able to render static html from a backbone view template.

Say we wanted to render a button like so:

<button type="button" class="btn">button</button>

Here is a very simple template which you would use in a static page.

{{{c 'btn' text="button"}}}

And here is the controller. It will always set the name of the controller as a class on the rendered tag.

class BtnStaticController extends marionette_lib.controllers.Static
  name: 'btn'
  tagName: 'button'
  attributes:
    type: 'button'

You can specify attributes and contextProperties to pass information through to the rendered html.

Defaults can be set for attributes and properties.

class BtnStaticController extends marionette_lib.controllers.Static
  name: 'btn'
  tagName: 'button'
  attributes:
    type: 'button'
  contextProperties:
    name: 'Unknown'

Then in your page you can use them:

{{{c 'btn' type="submit" name="Bob"}}}

And the underlying template would look like:

Hello {{name}}

To render

<button type="submit" class="btn">Hello Bob</button>

Entities

Handlebars

Routers

Utilities

Navigation

This encapsulates navigation helpers on the marionette_lib.navigation namespace.

  • .getCurrentRoute() return the current url fragment
  • .startHistory(options) start backbone history and set a flag to say it has started
  • .to(route, options) set the url fragment without triggering navigation unless specified in the options

Registry

Keep a registry of all controllers instantiated within the app so you can see if any are not being cleaned up.

class MyThing
  constructor: ->
    @_instance_id = _.uniqueId('thing')
    marionette_lib.registry.register(@, @_instance_id)
    super
  destroy: ->
    marionette_lib.registry.unregister(@, @_instance_id)

Views

Development

To make a release run grunt release