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
63
0
Share
Client-Side Data Modelling and more...
- Modelling in javascript
- Events
- Relationships
- Forms
Swaroop SM
January 06, 2016
More Decks by Swaroop SM
See All by Swaroop SM
ReactJS Awesomeness
swaroopsm
3
260
Testing JavaScript like a "BOSS"
swaroopsm
0
66
The Truth About Truthy & Falsy
swaroopsm
1
59
Other Decks in Programming
See All in Programming
From Formal Specification to Property Based Test
ohbarye
0
320
Back to the roots of date
jinroq
0
410
年間50登壇、単著出版、雑誌寄稿、Podcast出演、YouTube、CM、カンファレンス主催……全部やってみたので面白さ等を比較してみよう / I’ve tried them all, so let’s compare how interesting they are.
nrslib
4
800
GNU Makeの使い方 / How to use GNU Make
kaityo256
PRO
16
5.6k
Lightning-Fast Method Calls with Ruby 4.1 ZJIT / RubyKaigi 2026
k0kubun
3
1.2k
Going Multiplatform with Your Android App (Android Makers 2026)
zsmb
2
450
アーキテクチャモダナイゼーションとは何か
nwiizo
19
5.4k
Claude Code × Gemini × Ebitengine ゲーム制作素人WebエンジニアがGoでゲームを作った話
webzawa
0
160
Terraform言語の静的解析 / static analysis of Terraform language
wata727
1
110
How Swift's Type System Guides AI Agents
koher
0
300
Swift Concurrency Type System
inamiy
1
550
Claude Codeをカスタムして自分だけのClaude Codeを作ろう
terisuke
0
150
Featured
See All Featured
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
290
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
900
The Mindset for Success: Future Career Progression
greggifford
PRO
0
310
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1k
GraphQLとの向き合い方2022年版
quramy
50
15k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
110
Making the Leap to Tech Lead
cromwellryan
135
9.8k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
490
30 Presentation Tips
portentint
PRO
1
280
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
110
Google's AI Overviews - The New Search
badams
0
980
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
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
`