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
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
68
The Truth About Truthy & Falsy
swaroopsm
1
61
Other Decks in Programming
See All in Programming
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
790
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
280
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
260
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
360
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
Agentic UI
manfredsteyer
PRO
0
170
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
560
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
590
AI 輔助遺留系統現代化的經驗分享
jame2408
1
720
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
200
The NotImplementedError Problem in Ruby
koic
1
840
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
200
Featured
See All Featured
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.1k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
320
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
44k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
How GitHub (no longer) Works
holman
316
150k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.5k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
850
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
200
Building an army of robots
kneath
306
46k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
630
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.6k
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
`