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
59
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
260
Testing JavaScript like a "BOSS"
swaroopsm
0
59
The Truth About Truthy & Falsy
swaroopsm
1
55
Other Decks in Programming
See All in Programming
明示と暗黙 ー PHPとGoの インターフェイスの違いを知る
shimabox
2
390
deno-redisの紹介とJSRパッケージの運用について (toranoana.deno #21)
uki00a
0
170
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
150
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
220
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
370
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
1
570
新メンバーも今日から大活躍!SREが支えるスケールし続ける組織のオンボーディング
honmarkhunt
1
330
XP, Testing and ninja testing
m_seki
3
220
Select API from Kotlin Coroutine
jmatsu
1
210
VS Code Update for GitHub Copilot
74th
1
550
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
260
ソフトウェア品質を数字で捉える技術。事業成長を支えるシステム品質の マネジメント
takuya542
0
400
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Documentation Writing (for coders)
carmenintech
72
4.9k
Making the Leap to Tech Lead
cromwellryan
134
9.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
124
52k
Typedesign – Prime Four
hannesfritz
42
2.7k
Git: the NoSQL Database
bkeepers
PRO
430
65k
Testing 201, or: Great Expectations
jmmastey
42
7.6k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
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
`