presenter.html revision e8dfb26643f16b792ffe029dc404d89ce35b64b6
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo<!DOCTYPE html>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo<html>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo<head>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <meta charset="UTF-8" />
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <title>Y.App Using a Presenter</title>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo</head>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo<body>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <h1>Y.App Using a Presenter</h1>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <script id="t-user" type="text/x-handlebars-template">
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <ul>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <li><b>Fullname:</b> {{fullname}}</li>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <li><b>Username:</b> {{username}}</li>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo </ul>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <p><a href="#/edit/">Edit User</a></p>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo </script>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <script id="t-user-edit" type="text/x-handlebars-template">
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <form action="" method="POST">
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <fieldset>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <legend>User Info</legend>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <div>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <label for="f-fullname">Fullname:</label>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <input id="f-fullname" type="text" name="fullname" value="{{fullname}}" />
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo </div>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <div>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <label for="f-username">Username:</label>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <input id="f-username" type="text" name="username" value="{{username}}" />
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo </div>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo </fieldset>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <a href="#/">Cancel</a>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <input type="submit" value="Save" />
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo </form>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo </script>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <script src="/build/yui/yui.js"></script>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo <script>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo YUI({filter: 'raw'}).use('app', 'handlebars', function (Y) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo // -- Presenter --------------------------------------------------------
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo Y.Presenter = Y.Base.create('presenter', Y.Base, [], {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo viewType: Y.View,
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo initializer: function (config) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo config || (config = {});
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo if (Y.Lang.isString(config.viewType)) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.viewType = Y.Object.getValue(Y, config.viewType.split('.'));
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.after('containerChange', this._afterContainerChange);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.after('modelChange', this._afterModelChange);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.after('viewChange', this._afterViewChange);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.get('model').addTarget(this);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.get('view').addTarget(this);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo destroy: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo var model = this.get('model'),
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo view = this.get('view');
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo model.destroy.apply(model, arguments);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo view.destroy.apply(view, arguments);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo return Y.Presenter.superclass.destroy.apply(this, arguments);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo remove: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo var view = this.get('view');
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo view.remove.apply(view, arguments);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo return this;
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo render: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo var view = this.get('view');
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo view.render.apply(view, arguments);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo return this;
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo _getContainer: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo return this.get('view').get('container');
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo _setModel: function (model) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo if (!(model && model._isYUIModel)) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo model = new Y.Model(model);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo return model;
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo _setView: function (view) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo if (!view instanceof Y.View) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo view = new this.view(view);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo return view;
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo _afterContainerChange: function (e) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.get('view').set('container', e.newVal);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo _afterViewChange: function (e) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo e.prevVal && e.prevVal.removeTarget(this);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo e.newVal && e.newVal.addTarget(this);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo _afterModelChange: function (e) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo e.prevVal && e.prevVal.removeTarget(this);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo e.newVal && e.newVal.addTarget(this);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }, {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo ATTRS: {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo container: {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo getter: '_getContainer',
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo setter: '_setContainer'
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo model: {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo setter: '_setModel',
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo value : null
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo view: {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo setter: '_setView',
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo value : null
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo // -- User -------------------------------------------------------------
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo Y.namespace('MyApp').User = Y.Base.create('user', Y.Model, [], {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo idAttribute: 'username'
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }, {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo ATTRS: {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo fullname: {value: null},
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo username: {value: null}
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo // -- UserView -------------------------------------------------------------
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo Y.namespace('MyApp').UserView = Y.Base.create('userView', Y.View, [], {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo template: Y.Handlebars.compile(Y.one('#t-user').getContent()),
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo render: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo var content = this.template(this.get('model').toJSON());
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.get('container').setContent(content);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo return this;
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo // -- UserEditView ---------------------------------------------------------
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo Y.namespace('MyApp').UserEditView = Y.Base.create('userEditView', Y.View, [], {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo template: Y.Handlebars.compile(Y.one('#t-user-edit').getContent()),
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo events: {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo 'form' : {submit: 'saveUser'},
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo 'input[name=fullname]': {change: 'updateFullname'},
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo 'input[name=username]': {change: 'updateUsername'}
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo disableUsername: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.get('container').one('input[name=username]').set('disabled', true);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo render: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo var content = this.template(this.get('model').toJSON());
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.get('container').setContent(content);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo return this;
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo saveUser: function (e) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo e.preventDefault();
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.fire('saveUser', {user: this.get('model')});
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo updateFullname: function (e) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.fire('updateFullname', {fullname: e.target.get('value')});
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo updateUsername: function (e) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.fire('updateUsername', {username: e.target.get('value')});
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo // -- UserPresenter ----------------------------------------------------
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo Y.namespace('MyApp').UserPresenter = Y.Base.create('userPrsenter', Y.Presenter, [], {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo initializer: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.on('userEditView:saveUser', this.saveUser);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.on('userEditView:updateFullname', this.updateFullname);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.on('userEditView:updateUsername', this.updateUsername);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo render: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo Y.MyApp.UserPresenter.superclass.render.apply(this, arguments);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo if (!this.get('model').get('allowEditUsername')) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.get('view').disableUsername();
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo saveUser: function (e) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo var user = e.user,
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo model = this.get('model');
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo user.set('fullname', model.get('fullname'));
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo if (model.get('allowEditUsername')) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo user.set('username', model.get('username'));
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo user.save();
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo updateFullname: function (e) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.get('model').set('fullname', e.fullname);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo updateUsername: function (e) {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.get('model').set('username', e.username);
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo // -- App --------------------------------------------------------------
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo var app;
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo app = new Y.App({
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo serverRouting: false,
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo views: {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo show: {type: 'MyApp.UserView'},
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo edit: {type: 'MyApp.UserPresenter'}
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo },
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo routes: [
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo {path: '/', callback: 'showUser'},
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo {path: '/edit/', callback: 'editUser'}
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo ]
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo app.showUser = function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.showView('show', {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo model: this.user
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo };
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo app.editUser = function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo var user = this.user;
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo // Uses a Presenter!
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo this.showView('edit', {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo view : new Y.MyApp.UserEditView({model: user}),
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo model: {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo allowEditUsername: user.isNew()
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo };
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo app.user = new Y.MyApp.User({
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo after: {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo save: function () {
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo app.replace('/');
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo }
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo app.render().replace('/');
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo });
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo </script>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo</body>
e8dfb26643f16b792ffe029dc404d89ce35b64b6Eric Ferraiuolo</html>