GithubHelp home page GithubHelp logo

invoca / backdraft Goto Github PK

View Code? Open in Web Editor NEW
5.0 71.0 2.0 5.56 MB

Wrapper around Backbone providing jQuery dataTable integration and plugin framework

License: MIT License

JavaScript 93.55% HTML 3.87% CSS 2.58%

backdraft's Introduction

Backdraft

Wrapper around Backbone providing integration with DataTables, as well as other utilities for creating Backbone views and models. Written as a plugin-based framework, where the DataTables integration is a plugin itself, and Backdraft can be further extended with your own plugins.

Install

npm install backdraft-app

Usage

First, define a new Backdraft app:

// app.js
import MainRouter from "./main_router";

import App from "backdraft-app/src/app";

class TableExample extends App {
  activate($el) {
    this.mainRouter = new MainRouter({ $el });
    Backbone.history.start({ });
  }
}

Define an entry point for creating your app:

// main.js
import TableExample from "./app";

Backdraft.app("TableExample", new TableExample());

Define the various components of your app:

// main_router.js
import IndexView from "./views/index_view";

import Router from "backdraft-app/src/router";

export default class MainRouter extends Router {
  get routes() {
    return {
      "" : "index"
    };
  }

  index() {
    const view = new IndexView();
    this.swap(view);
  }
};

// models/book.js
import Model from "backdraft-app/src/model";

export default class Book extends Model {
};

// collections/books.js
import Book from "../models/book";

import Collection from "backdraft-app/src/collection";

export default class Books extends Collection {
  get model() {
    return Book;
  }
};

// views/book_table_view.js
import BookRowView from "./book_row_view";

import LocalDataTable from "backdraft-app/src/data_table/local_data_table";

export default class BookTableView extends LocalDataTable {
  get rowClass() {
    return BookRowView;
  }
  
  get paginate() {
    return false;
  }
};

// views/book_row_view.js
import Row from "backdraft-app/src/data_table/row";

export default class BookRowView extends Row {
  get columns() {
    return [
      { bulk : true },
      { attr : "name", title : "Name" },
      { attr : "created_at", title : "Created" }
    ];
  }
};

Now create the view that pulls all the pieces together:

// views/index_view.js
import BookTableView from "./book_table_view";
import Books from "../collections/books";

import View from "backdraft-app/src/view";

export default class IndexView extends View {
  render() {
    const collection  = new Books();
    const data = [];
  
    // fake data
    for (let iter = 0; iter < 10; ++iter) {
      data.push({ name : `Book ${iter + 1}` });
    }
  
    collection.add(data);
    const table = new BookTableView({ collection });
  
    this.$el.html(table.render().$el);
    return this;
  }
}

Finally, in an HTML page that loads the above scripts, activate the app at load time:

<html>
  <head>
    ...
  </head>
  <body>
    <div id="example-area"></div>

    <script type="text/javascript">
      Backdraft.app("TableExample").activate($("#example-area"));
    </script>
  </body>
</html>

Legacy Usage

The legacy usage uses the Backdraft object to define the components of the application.

First, define a new Backdraft app and what plugins it will use:

Backdraft.app("TableExample", {
  plugins : ["DataTable"],

  activate : function($el) {
    this.mainRouter = new this.Routers.Main({ $el : $el });
    Backbone.history.start({ });
  }
});

Now, everything else gets namespaced within the Backdraft app name:

// routers/main.js
Backdraft.app("TableExample", function(app) {
  app.router("Main", {
    routes : {
      "" : "index"
    },

    index : function() {
      var view = new app.Views.Index();
      this.swap(view);
    }
  });

});

// models/book.js
Backdraft.app("TableExample", function(app) {
  app.model("Book", {});
});

// collections/book.js
Backdraft.app("TableExample", function(app) {
  app.collection("Books", {
    model : app.Models.Book
  });
});

// views/book_table.js
Backdraft.app("TableExample", function(app) {
  app.view.dataTable("BookTable", {
    rowClassName : "BookRow",
    paginate : false
  });
});

// views/book_row.js
Backdraft.app("TableExample", function(app) {
  app.view.dataTable.row("BookRow", {
    columns : [
      { bulk : true },
      { attr : "name", title : "Name" },
      { attr : "created_at", title : "Created" }
    ]
  });
});

Now create the view that pulls all the pieces together:

// views/index.js
Backdraft.app("TableExample", function(app) {
  app.view("Index", {
    render : function() {
      var collection  = new app.Collections.Books();
      var data = [];

      // fake data
      for (var iter = 0; iter < 10; ++iter) {
        data.push({ name : "Book " + (iter + 1) });
      }

      collection.add(data);
      var table = new app.Views.BookTable({ collection : collection });

      this.$el.html(table.render().$el);
      return this;
    }
  });
});

Finally, in an HTML page that loads the above scripts, include the dist/backdraft.js file in your assets. You must also include the required vendor files (jQuery & underscore.js), plus dataTables.js if using the datatables plugin.

Then activate the app at load time:

<html>
  <head>
    ...
  </head>
  <body>
    <div id="example-area"></div>

    <script type="text/javascript">
      Backdraft.app("TableExample").activate($("#example-area"));
    </script>
  </body>
</html>

Examples

If you run yarn run examples, a local server will be launched with live examples at localhost:9888.

Develop & Contribute

Environment Setup

To develop a Backdraft plugin or modify Backdraft, the following setup needs to be done first (this assumes a Mac running OS 10.9+):

  • Install NVM, the Node Version Manager https://github.com/creationix/nvm

  • curl https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | sh

  • Open a new tab in your terminal

  • Install Node.js with:

  • nvm install v6.11.0

  • Set it as the default Node.js version

  • nvm alias default v6.11.0

  • Install Yarn https://yarnpkg.com/en/docs/install

  • Install dependencies

    cd backdraft
    yarn run setup
    yarn install
    

Testing

Run the yarn dev script from the main directory to have the source and test files watched and tests auto-run when any modifications are done

yarn run dev

Run the specs script to run all the tests and exit

yarn run specs

Use the following two ENV variables to target specific tests:

  • SPEC_FILTER - target specific tests via karma-jasmine's --grep feature.

  • SPEC_FILE_FILTER - target specific tests by file path. This value must be a regex. For example:

    SPEC_FILE_FILTER="/data_table.*_spec.js/" yarn run specs

See all available commands with:

yarn run

Contributing

If you'd like to contribute a feature or bugfix: Thanks! To make sure your change has a high chance of being included, please read the following guidelines:

  1. Post a pull request.
  2. Please add tests. We will not accept any patch that is not tested. (It's rare when explicit tests aren't needed.) Please see existing tests for examples and structure.

Thank you to all the contributors!

Publishing - admins

To publish a new version to NPM (https://www.npmjs.com/package/backdraft-app), do the following

  1. Ensure the following are complete:
  • Tested
  • Green build
  • Code Reviewed
  1. Publish new version: yarn version --new-version <version>:
  • creates commit with version change
  • tags the commit with the version
  • pushes the commit and tag to Github
  • builds and publishes the node module
  1. If on a branch, ensure this version gets merged to master

License

Backdraft is Copyright © 2014-2018 Invoca, Inc. MIT license.

backdraft's People

Contributors

nburwell avatar rahulk1000 avatar mileszim avatar alecjacobs5401 avatar euge avatar gabe-kent avatar dtognazzini avatar albertovillalobos avatar vicentecarrillo avatar codyjca avatar trevorfrese avatar echistyakov avatar hparker avatar colin-allen avatar jebentier avatar codingiam avatar ibiziiac-sv avatar scottpchow23 avatar mikeweaver avatar scommette avatar dependabot[bot] avatar

Stargazers

Leonardo Escudero avatar Dan Groza avatar Fabien Franzen avatar  avatar  avatar

Watchers

 avatar Robert Prince avatar Victor Borda avatar  avatar James M. Buehring avatar Mike McCourt avatar Dan Kozlowski avatar John Breen avatar  avatar Kenneth Mitchner avatar James Cloos avatar Orion Osborn avatar  avatar  avatar Alexander Schreifels avatar Tim Dupree avatar Ryan Bales avatar Dan Lloyd avatar baki avatar Brandon Bell avatar  avatar Muzafar Umarov avatar  avatar Dan Bradbury avatar Jasen Hall avatar Oleksiy Blavat avatar  avatar Christina Lesnick avatar Dan Groza avatar  avatar Greg Meyer avatar Bobby Kearns avatar Laura Smith avatar Paul Thomas avatar Christian Parkinson avatar Iryna Shakun avatar Susan Asan avatar Michael Daigle avatar Lia Stahl avatar  avatar Deepti Jain  avatar Luis Aragon avatar matt avatar Amy avatar Daniel Pramann avatar Keith Hall avatar  avatar Nico Moreno avatar  avatar Bryan Meeker avatar Keith Bryant avatar Michael Crozier avatar Pooja V Kadam avatar Justin Bell avatar Alex Kang avatar  avatar Brendan Brown avatar Mateo Wang avatar  avatar Brandon Novick avatar Tristan Starck avatar Michael Casciato avatar Carson Hoff avatar Riley Thompson avatar Eli Echt-Wilson avatar Trevor Storey avatar Jenn Chu avatar  avatar Anneke McGrady avatar Marissa avatar Khalil Shakir avatar

Forkers

euge isabella232

backdraft's Issues

Add ability to specify sort by attr

From @nburwell

actually, allow sorting by the attr name would be my favorite. Then if there were more than one column using the same attr, just pick the first visible one to set the dataTable sort setting on =D

Backdraft needs to look for 4xx responses and handle them appropriately

For example, a user who logs out and back in to the app in a separate tab will invalidate the CSRF token submitted by the insights app. Right now the app doesn't handle 401/403 responses and simply acts as though nothing happened (see slot deletion for an example).

404 is going to be a different case, but that one might be worth looking at as well?

I don't know how many other specific handlers we'd want, but we should probably also have a catch-all for responses in the 4xx and 5xx range.

Overhaul to ES6

Benefits :

  1. Distribution. Distributing as ES6 modules greatly improves flexibility for consumers. We can continue to ship ES5 as well if we're concerned about old school consumers.

  2. Development. Working with ES6 increases productivity and reduces bugs.

Declare dependencies

Currently, no dependencies are declared for the backdraft-app node module. Dependent libraries have been copied into vendor/.

The task here is to declare dependencies in package.json. Dependencies only necessary for tests should be declared as devDependencies.

Support DataTables 1.10.x

Currently the dataTables plugin is designed for DataTables 1.9.x

Provide support for 1.10.x, as many 3rd party plugins (such as filtering), only work with 1.10+. Latest right now is 1.10.7.

@euge, would love to get your help on this one =)

Throw error when doing a row render and nodes.length > 1

There is a potential bug with css class names being duplicate and matching more than 1 td element.

Example:
Column Names: "Bob & Joe" and "Bob % Joe"
The css class names for these two cells would be identical.
We should probably replace invalid characters with their ascii equivalents.
"%".charCodeAt(0) => 37

Use browserlist?

Should we use browserlist to specify supported browsers when building dist files?

Consumers that import ES6 modules won't need this. This would only affect down-rev consumers that consume the dist.

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.