Amaze UI 2.4

JavaScript

Amaze UI styled jQuery plugins and utils.

JavaScript


Basic Usage

Based on jQuery

Amaze UI JavaScript Components require jQuery since version 2.0. Please make sure the latest version of jQuery is loaded before Amaze UI script.

Using Components

Using Amaze UI JS Components is just like using other jQuery plugins. Details can be found in documents of each component.

Differences between jQuery and Zepto.js

jQuery and Zepto.js seems to be similar, but actually they are different in detail. Supporting jQuery and Zepto.js is not that worthy to do. Maybe that's why Foundation 5 dropped Zepto. (Difference Demo listed below

width()/height()

  • Zepto.js: Decided by box model(box-sizing).
  • jQuery: Dismiss box model, always return width/height of content(not include padding and border)

jQuery Official Explanation

Note that .width() will always return the content width, regardless of the value of the CSS box-sizing property. As of jQuery 1.8, this may require retrieving the CSS width plus box-sizing property and then subtracting any potential border and padding on each element when the element has box-sizing: border-box. To avoid this penalty, use .css("width") rather than .width().

Solution is using .css('width') instead of .width() in jQuery.

Well, what jQuery does here is questionable, For example, $('.box').css('height') will return 20px, Whaaaaaat the fk**?

<style>
  .box {
    box-sizing: border-box;
    padding: 10px;
    height: 0;
  }
</style>

<div class="box"></div>
Getting height and width of a triangle

Suppose we draw a small lovely triangle using following HTML and CSS:

<div class="caret"></div>
.caret {
  width: 0;
  height: 0;
  border-width: 0 20px 20px;
  border-color: transparent transparent blue;
  border-style: none dotted solid;
}
  • In jQuery, using .width() and .css('width') will return 0, same to height.
  • In Zepto, using .width() returns 40, but using .css('width') returns 0px

So, in this case, use .outerWidth()/.outerHeight() in jQuery; use .width()/.height() in Zepto

offset()

  • Zepto.js: Return top, left, width, height
  • jQuery: Return width, height

$(htmlString, attributes)

Difference in DOM Manipulation
$(function() {
  var $list = $('<ul><li>jQuery Insertion</li></ul>', {
    id: 'insert-by-jquery'
  });
  $list.appendTo($('body'));
});

In jQuery, id won't be added to ul; In Zepto, it will。

Difference in event trigger
$script = $('<script />', {
  src: 'https://amazeui.github.io/amazeui/1.0.1/js/amazeui.min.js',
  id: 'ui-jquery'
});

$script.appendTo($('body'));

$script.on('load', function() {
  console.log('jQ script loaded');
});

In jQuery, the process function of load event won't be executed; In Zepto, It will。

其他参考链接:

Advanced Usage

Default Initialize Events Interface

Amaze UI use specific HTML to bind events. Most of the JS components can be used simply by using HTML tags.

Default initialize events are all in namespace xx.amui.data-api. Developers can turn them off as their will.

Turn off all default events:

$(document).off('.data-api');

Turn off default events for specific component:

$(document).off('.modal.amui.data-api');

Customized Events

Some components provide customized events, which is named by {事件名称}.{组件名称}.amui. Read documents of components for more details.

$('#myAlert').on('close.alert.amui', function() {
  // do something
});

MutationObserver

Two-way data binding is cool? Mutation Observer is (or will be) the unsung hero.

We experimentally include MutationObserver in Amaze UI 2.1 , Be careful using it.

data-am-observe

After adding data-am-observe to an element, Amaze UI JS plugins that dynamiclly inserted will be automatically initiallized(Example), Supported plugins: Button、Dropdown、Slider、Popover、ScrollSpy、Tabs。

$().DOMObserve(options, callback)

  • options: Attributes of observing(Details). Default: {childList: true, subtree: true}
  • callback(mutations, observer): Processing function when DOM changes. The first parameter is the array that stores MutationRecord object; the second parameter is the MutationObserver instance itself。

Example of DOM change observing. See console for log.

<p id="js-do-actions">
  <button class="am-btn am-btn-primary" data-insert>Insert p element</button>
  <button class="am-btn am-btn-secondary" data-addClass>Add Class</button>
  <button class="am-btn am-btn-warning" data-remove>Remove p element</button>
</p>
<div id="js-do-demo">
  <p>Example of DOM change observing. See console for log.</p>
</div>
<script>
  $(function() {
    var $wrapper = $('#js-do-demo');
    $wrapper.DOMObserve({
      childList: true,
      attributes: true,
      subtree: true
    }, function(mutations, observer) {
      console.log(observer.constructor === window.MutationObserver);
      console.log('DOM of #js-do-demo is changing: ' + mutations[0].type);
    });

    $('#js-do-actions').find('button').on('click', function(e) {
      var $t = $(e.target);
      if ($t.is('[data-insert]')) {
        $wrapper.append('<p>a p is inserted</p>');
      } else if($t.is('[data-remove]')) {
        $wrapper.find('p').last().remove();
      } else {
        $wrapper.addClass('am-text-danger');
      }
    });
  })
</script>

Reference:

Modular Development

We did an survey about front-end JS modularization, when Amaze UI was in version 1.0 . Until Nov 13th, 2014, we recieved 1869 votes:

  • CMD - Sea.js 23.86% (446 votes)
  • AMD - RequireJS 24.51% (458 votes)
  • CommonJS - Browserify 9.58% (179 votes)
  • Other(or develop a new one) 8.19% (153 votes)
  • What is JS Modularization? Some sort of dessert? 34% (633 votes)
CMD
AMD
CJS
other
unknown

Obviously, Modularization is the future. ES6 will have native support for modularization.

Amaze UI 2.0 follows CommonJS standard to organize modules(write modules like node.js in front-end). Developers can choose package tools they like.

  • Browserify:Work with NPM, Browserify can manage front-end modules. Many front-end modules have been published to NPM, so we can drop some tools like Bower;
  • Duo:Besides managing local modules, Duo can help us fetch open source projects directly from GitHub. Support Javascript, HTML and CSS;
  • gulp-amd-bundler:Package modules coded in CJS to AMD modules;
  • Webpack

SPM Can't extract dependancies from source codes. If you are are using Sea.js, you probably need to modify your package tool by yourself.

Further Reading:

侧栏导航
Amaze UI 微信
在微信上关注我们