Code Monkey home page Code Monkey logo

mvc.js's Introduction

mvc.js

A lightweight MVC framework inspired by AngularJS (not Angular 2).

This is mostly a trimmed-down version of AngularJS with a few important differences that make it more usable. It is also more efficient because it relies on Proxy objects to detect changes rather than evaluating the entire model every time.

Bootstrap

Import the latest build and initialize mvc.js by adding the mvc-app attribute to an element of your choice (typically the document body):

<html>
<head>
  ...
  <script src="mvc.min.js"></script>
</head>
<body mvc-app>
  ...
</body>
</html>

Much like in AngularJS, you can use mvc-cloak to prevent showing template DOM before mvc.js has a chance to render it, but you'll have to style it on your own (mvc.js only removes the mvc-cloak attribute or CSS class after rendering).

<html>
<head>
  ...
  <style>
    [mvc-cloak] {
      display: none !important;
    }
  </style>
  <script src="mvc.min.js"></script>
</head>
<body mvc-app mvc-cloak>
  ...
</body>
</html>

Directives

Much like AngularJS, mvc.js has directives that affect the HTML elements to which they're attached and can be specified as HTML attributes (e.g. mvc-if) or elements (e.g. mvc-include).

When a DOM tree is bound to a model (either explicitly with MVC.bind() or implicitly using mvc-app) the tree is walked depth-first and for each node, each registered directive decides whether or not it can attach itself to the node. When a directive attaches itself to a node it takes responsibility for any changes performed on the node while keeping in mind that the next directives in the chain may also decide to attach themselves to the same node and perform other changes. Possible changes include even eliminating the node from the tree (cfr. mvc-if) or replicating it many times (cfr. mvc-for).

The order in which existing directives are executed on each node is well defined and it's very important to determine how each element works, e.g. what fields can be fecthed from the model by each directive. Built-in directives are executed in the following order:

  1. mvc-with-*
  2. mvc-for
  3. mvc-if
  4. mvc-controller
  5. mvc-include
  6. mvc-on-*
  7. Two-way data binding

This means, for example, that mvc-if can access fields that are added to the model by mvc-for, such as the iteration variable, the $index variable, and so on, because mvc-if runs after mvc-for.

Built-in Directives

Two-way data binding

mvc.js supports the double curly bracket syntax for HTML attributes and text elements. They get updated automatically as the model changes. Example:

<p mvc-controller="MyController" class="first-class {{secondClass}} third-class">
  The quick brown {{jumper}} jumps over the lazy {{jumpee}}.
</p>
MVC.Controllers.register(function MyController(model) {
  model.secondClass = 'second-class';
  model.jumper = 'fox';
  model.jumpee = 'dog';
});

In the above we used mvc-controller to change the secondClass, jumper, and jumpee variables in the model.

mvc-on-*

TBD

mvc-include, mvc-transclude

mvc-include elements are automatically replaced with the HTML code of the specified template. Templates are identified by unique names and can be either provided to the JavaScript API or as HTML5 <template> elements.

The following:

<body mvc-app>
  <mvc-include template="template1"></mvc-include>
  <mvc-include template="template2"></mvc-include>
  <template id="template2">
    <p>Paragraph 2.</p>
  </template>
</body>
MVC.Templates.registerFromString('template1', `
  <p>Paragraph 1</p>
`);

is equivalent to the following:

<body>
  <p>Paragraph 1.</p>
  <p>Paragraph 2.</p>
</body>

Templates may contain other directives. The following:

<body mvc-app>
  <mvc-include template="paragraphs"></mvc-include>
  <template id="paragraphs">
    <p mvc-for="i in [1, 2, 3]">Paragraph {{i}}.</p>
  </template>
</body>

is equivalent to:

<body>
  <p>Paragraph 1.</p>
  <p>Paragraph 2.</p>
  <p>Paragraph 3.</p>
</body>

The content of an mvc-include element itself is transcluded in the template if the template has one or more transclusion points. Transclusion points are marked by the <mvc-transclude> element. For example:

<body mvc-app>
  <mvc-include template="dialog">
    <p>The quick brown fox jumps over the lazy dog.</p>
  </mvc-include>
  <template id="dialog">
    <dialog open>
      <mvc-transclude></mvc-transclude>
    </dialog>
  </template>
</body>

is equivalent to:

<body>
  <dialog open>
    <p>The quick brown fox jumps over the lazy dog.</p>
  </dialog>
</body>

mvc-controller

TBD

mvc-if

Adds or removes a DOM node conditionally based on an expression.

Example:

<body mvc-app>
  ...
  <div mvc-if="var1 && var2">
    ...
  </div>
  ...
</body>

The <div> element and its content are kept in the DOM iff both model fields var1 and var2 are true (or other non-falsey JS values).

mvc-for

TBD

mvc-with-*

TBD

Defining Custom Directives

TBD

mvc.js's People

Contributors

71104 avatar

Watchers

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