Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Client-Side Data Modelling and more...
Search
Swaroop SM
January 06, 2016
Programming
0
54
Client-Side Data Modelling and more...
- Modelling in javascript
- Events
- Relationships
- Forms
Swaroop SM
January 06, 2016
Tweet
Share
More Decks by Swaroop SM
See All by Swaroop SM
ReactJS Awesomeness
swaroopsm
3
250
Testing JavaScript like a "BOSS"
swaroopsm
0
54
The Truth About Truthy & Falsy
swaroopsm
1
51
Other Decks in Programming
See All in Programming
Implementing Design Systems in Swift
seyfoyun
1
460
Exploring the Implementation of “t.Run”, “t.Parallel”, and “t.Cleanup”
akarin
1
110
初心者のためのRubyKaigi入門/RubyKaigi Introduction
a_matsuda
10
1.4k
Milestoner
bkuhlmann
1
410
ServerAction で Progressive Enhancement はどこまで頑張れるか? / progressive-enhancement-with-server-action
takefumiyoshii
6
420
StoreKit2によるiOSのアプリ内課金のリニューアル
kangnux
0
120
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
9
4.2k
Elm 0.19.0 Changes
bkuhlmann
0
510
敵対的ポイフル
futabato
0
130
Goのmultiple errorsについて (2024年4月版)
syumai
4
1.2k
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
4
900
PostmanでAPIの動作確認が楽になった話
h455h1
0
180
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
155
14k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
228
16k
Git: the NoSQL Database
bkeepers
PRO
423
63k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
65
14k
Art, The Web, and Tiny UX
lynnandtonic
290
19k
Six Lessons from altMBA
skipperchong
22
3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
323
20k
What the flash - Photography Introduction
edds
64
11k
The MySQL Ecosystem @ GitHub 2015
samlambert
244
12k
The Cult of Friendly URLs
andyhume
74
5.7k
Build your cross-platform service in a week with App Engine
jlugia
226
17k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.7k
Transcript
Client-Side Data Modeling Swaroop “STRIKER” Sethumadhavan @smswaroop
What is this about?
…obviously
…Again, what is it about? • Data Modeling • Validations
• Better form submissions • AJAX • DRY and Re-usable code • …more
Model • Heart of any application • A single unit
• Business Logic
A Simple JS Model function Person() { this.firstName = null;
this.lastName = null; this.age = null; this.getFullName = function() { return [ this.firstName, this.lastName ].join(' '); }; this.isAllowedToBooze = function() { return this.age && this.age > 18; }; }
Adding Validations to Model function Person() { // Private variables
var AGE_LIMIT = 13, hasErrors = false; this.firstName = null; this.errors = {}; .... this.canRegister = function() { return this.age && this.age >= AGE_LIMIT; }; // Validations this.isValid = function() { this.validate(); // And some logic here } this.validate = function() { if(!this.firstName) { addErrorFor('firstName', 'can\'t be empty'); } if(!this.age) { addErrorFor('age', 'can\'t be empty') } if(this.age && !this.canRegister()) { addErrorFor('age', 'can\'t be lesser than ' + AGE_LIMIT + ' years') } }; // Private Methods var addErrorFor = function(key, msg) { this.errors[key] = this.errors[key] || []; this.errors[key].push(msg); } }
Forms
An invite form <form action=“/invite"> <label for="email">Email:</label> <input type="email" id="email"
data-field="email" name="email"> <label for="firstName">Firstname:</label> <input type="text" id="firstName" data-field="firstName" name="firstName"> <label for="age">Age:</label> <input type="number" id="age" data-field="age" name="age"> </form>
Initiate the view window.app = window.app || {}; app.inviteForm =
{ $el: null, init: function($el) { this.$el = $el; this.model = new Person(); // Assign attributes // Probably use jQuery } }; app.inviteForm.init(document.getElementById('form'));
Assign Attributes init: function() { this.$el.find(‘:input').each(function(index, field) { var $field
= $(field), attr = $field.data('field'), val = attr.val(); if(attr) { this.model[attr] = val; } }.bind(this)); }
Submitting Form init: function() { ..... this.$el.submit(function(e) { if(this.model.isValid()) {
// Normal Form Submit return true; } e.preventDefault(); // Render error messages }.bind(this)); }
Submit Form via AJAX function Person() { ... this.save =
function() { $.ajax({ .... }) } } init: function() { ..... this.$el.submit(function(e) { if(this.model.isValid()) { this.model.save(); } }.bind(this)); }
Callbacks save: function() { $.ajax({ success: function() { this.afterSave(); }.bind(this)
}); }, afterSave: function() { console.log('Finished Saving') } save: function() { this.beforeSave(function() { $.ajax({ success: function() { this.afterSave(); }.bind(this) }) }); }, beforeSave: function(callback) { console.log('Before Saving'); callback(); }
Relations hasOne: function() { return { name: 'address', className: Address
} }, hasMany: function() { return { name: 'kids', className: Person } }, belongsTo: function() { return { name: 'company', className: 'Company' } }
Events • Subscribe for any events (Eg.: User avatar uploaded)
• Publish (inform all the subscribers once uploaded) • Refresh respective views
Subscribe to Event window.app = window.app || {}; app.subscribers =
[]; app.subscribe = function(name, fn) { app.subscribers.push(name, fn); } app.publish = function(event) { var args = Array.prototype.slice.call(arguments, 1); // Find all subscribers and call subscriber[0].call(args); } app.inviteForm = { $el: null, init: function($el) { app.subscribe(‘person.avatar.upload’, function(url) { // Refresh the view }) } }; app.inviteForm.init(document.getElementById('form'));
Publish Event save: function() { $.ajax({ success: function() { app.publish(‘person.avatar.upload',
'<url>'); } }); }
Frameworks • BackboneJS • Ember Data • …many more I
guess! :D
`