Skip to content

Instantly share code, notes, and snippets.

@johnwesonga
Last active August 29, 2015 13:57
Show Gist options
  • Save johnwesonga/9740345 to your computer and use it in GitHub Desktop.
Save johnwesonga/9740345 to your computer and use it in GitHub Desktop.
Very Simple FlightJS app
// components/ui/add_profile.js
define(function (require) {
'use strict';
/**
* Module dependencies
*/
var defineComponent = require('flight/lib/component');
/**
* Module exports
*/
return defineComponent(addProfile);
/**
* Module function
*/
function addProfile() {
this.defaultAttrs({
firstname: '#firstname',
lastname: '#lastname',
btnAddProfile: '#btnAddProfile'
});
this.createProfile = function(e) {
e.preventDefault();
var firstname = this.select('firstname');
var lastname = this.select('lastname');
this.trigger('uiAddProfile',{
firstname: firstname.val().trim(),
lastname: lastname.val().trim(),
});
firstname.val('');
lastname.val('');
}
this.after('initialize', function () {
this.on(document, 'submit', this.createProfile);
});
}
});
define(function (require) {
'use strict';
/**
* Module dependencies
*/
var ProfileData = require('component/data/profile');
var NewProfileUi = require('component/ui/add_profile');
var ListProfilesUi = require('component/ui/list_profile');
/**
* Module exports
*/
return initialize;
/**
* Module function
*/
function initialize() {
ProfileData.attachTo(document);
NewProfileUi.attachTo('#frmProfile');
ListProfilesUi.attachTo('#profileList');
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>helloworld</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/css/main.css">
</head>
<body>
<h2>User Profile</h2>
<form id="frmProfile">
First name: <input type="text" name="firstname" id="firstname"> <br>
Last name: <input type="text" name="lastname" id="lastname"><br/>
<input type="submit" value="Submit" id="btnAddProfile">
</form>
<br/>
<ul id="profileList">
</ul>
<!--[if lt IE 9]>
<script src="/bower_components/es5-shim/es5-shim.js"></script>
<script src="/bower_components/es5-shim/es5-sham.js"></script>
<![endif]-->
<script src="/bower_components/jquery/dist/jquery.js"></script>
<script src="/bower_components/requirejs/require.js" data-main="js/main.js"></script>
</body>
</html>
// components/ui/list_profile.js
define(function (require) {
'use strict';
/**
* Module dependencies
*/
var defineComponent = require('flight/lib/component');
/**
* Module exports
*/
return defineComponent(listProfile);
/**
* Module function
*/
function listProfile() {
this.listProfiles = function(e, data){
this.$node.append('<li>' + data.firstname + ' ' + data.lastname + '</li>');
}
this.after('initialize', function () {
this.on(document, 'dataAddProfile', this.listProfiles);
});
}
});
// components/data/profile.js
define(function (require) {
'use strict';
/**
* Module dependencies
*/
var defineComponent = require('flight/lib/component');
/**
* Module exports
*/
return defineComponent(profile);
/**
* Module function
*/
function profile() {
this.AddProfile = function(e, data){
this.trigger('dataAddProfile',
{firstname: data.firstname, lastname: data.lastname});
}
this.after('initialize', function () {
this.on(document, 'uiAddProfile', this.AddProfile);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment