Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why YUI3

ningzbruc
September 25, 2013

Why YUI3

Why we use YUI3?

ningzbruc

September 25, 2013
Tweet

More Decks by ningzbruc

Other Decks in Technology

Transcript

  1. <!DOCTYPE html> <html> <head> <script src="jquery.js"></script> <script src="a.js"></script> <script src="A.js"></script>

    <script src="b.js"></script> <script src="Ab.js"></script> </head> <body> <!-- More Code --> </body> </html> 还记得那⼀一年...
  2. YUI({ combine: false }).use('node', function(Y) { doSomething(); }); 取消 Combo

    <script src="http://yui.yahooapis.com/3.9.1/build/yui/yui-min.js"></script>
  3. YUI.add('hello', function(Y) { Y.Hello = { say: function() { console.log('hello');

    } }; }, '0.0.1', { requires: ['node', 'event'] }); 注册模块 hello.js
  4. YUI({ modules: { 'hello': { path: 'hello/hello.js', requires: ['node', 'event']

    } } }).use('hello', function(Y) { Y.Hello.say(); //hello }); 配置模块 page.html :)
  5. YUI().use('hello', function(Y) { Y.Hello.say(); }); YUI.add('hello', function(Y) { Y.Hello =

    { say: function() { console.log('hello'); } }; }, '0.0.1', { requires: ['node', 'event'] }); Y?
  6. YUI().use('hello', function(Y) { Y.Hello.say(); }); YUI.add('hello', function(Y) { Y.Hello =

    { say: function() { console.log('hello'); } }; }, '0.0.1', { requires: ['node', 'event'] }); Y?
  7. YUI().use('node', function(Y) { YUI().use('node', function(Y2) { Y.log(Y2 == Y); //false

    }); }); 强沙箱 YUI().use('node', function(Y) { Y.use('node', function(Y2) { Y.log(Y2 == Y); //true }); });
  8. ✓ 模块解耦 ✓ 按需加载 ✓ 异步加载 ✓ 减少HTTP请求 ✓ 强沙箱机制

    ✓ 处理依赖关系 ✓ 模块顺序执⾏行 ✓ 模块信息配置 YUI3模块系统
  9. function Animal() {} Animal.prototype.move = function() { Y.log('move'); }; extend

    function Cat() { Cat.superclass.constructor.apply(this, arguments); } Y.extend(Cat, Animal); var cat = new Cat(); cat.move(); //move
  10. function Fly() {} Fly.prototype.fly = function() { Y.log('fly'); }; augment

    Y.augment(Cat, Fly); var cat = new Cat(); cat.fly(); //fly
  11. function Optimus() { Optimus.superclass.constructor.apply(this, arguments); } Optimus.NAME = 'optimus'; Optimus.ATTRS

    = {}; Y.extend(Optimus, Y.Base, { initializer: function() {}, destructor: function() {} }); LifeCycle
  12. function Optimus() { Optimus.superclass.constructor.apply(this, arguments); } Optimus.NAME = 'optimus'; Optimus.ATTRS

    = {}; Y.extend(Optimus, Y.Base, { initializer: function() { this.publish('rollout'); }, destructor: function() {}, commandTo: function(where) { this.fire('rollout', { where: where }); } }); Custom Event
  13. var optimus = new Optimus(); optimus.commandTo('USA'); optimus.js Custom Event Y.on('optimus:rollout',

    function(e) { bumblebee.rollout(e.where); }); bumblebee.js Optimus.NAME = 'optimus';
  14. Optimus.ATTRS = { node: { getter: function(v) { return v;

    }, setter: function(v) { return Y.one(v); }, value: null } }; Attribute
  15. var optimus = new Optimus(); optimus.run(); optimus.run(); optimus.run(); optimus.js Custom

    Event Y.on('optimus:run', function(e) { bumblebee.transform(); //3 times bumblebee.run(); }); bumblebee.js
  16. Optimus.ATTRS = { running: { getter: function(v) { return v;

    }, setter: function(v) { return !!v; }, value: false } }; Attribute
  17. Attribute Optimus.ATTRS = { running: { getter: function(v) { return

    v; }, setter: function(v) { return !!v; }, value: false } }; Y.extend(Optimus, Y.Base, { initializer: function() { this.after('runningChange', function(e) { this.fire(e.newVal ? 'run' : 'stop'); }); }, run: function() { this.set('running', true); }, stop: function() { this.set('running', false); } });
  18. var optimus = new Optimus({running: null}); optimus.run(); optimus.run(); optimus.run(); optimus.js

    Attribute Y.on('optimus:run', function(e) { bumblebee.transform(); //1 time bumblebee.run(); }); bumblebee.js
  19. Y.Base.create('optimus', Y.Base, [ OptimusHead, OptimusBody, OptimusFooter ], { initializer: function()

    {}, destructor: function() {}, run: function() {}, stop: function() {} }, { ATTRS: {} }); Base.create
  20. • 分⼯工明确 - 模块化开发 • ⽆无缝配合 - 事件通信 • 接⼝口清晰

    - API清晰统⼀一 • 磨合简单 - ⽂文档完善 • ⾓角⾊色灵活 - 代码⻛风格统⼀一 多⼈人协作
  21. Q&A