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
61
The Truth About Truthy & Falsy
swaroopsm
1
56
Other Decks in Programming
See All in Programming
今だからこそ入門する Server-Sent Events (SSE)
nearme_tech
PRO
3
240
Swift Updates - Learn Languages 2025
koher
2
490
より安全で効率的な Go コードへ: Protocol Buffers Opaque API の導入
shwatanap
2
360
アルテニア コンサル/ITエンジニア向け 採用ピッチ資料
altenir
0
110
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
MCPでVibe Working。そして、結局はContext Eng(略)/ Working with Vibe on MCP And Context Eng
rkaga
5
2.3k
私の後悔をAWS DMSで解決した話
hiramax
4
210
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
560
さようなら Date。 ようこそTemporal! 3年間先行利用して得られた知見の共有
8beeeaaat
3
1.5k
はじめてのMaterial3 Expressive
ym223
2
880
OSS開発者という働き方
andpad
5
1.7k
プロポーザル駆動学習 / Proposal-Driven Learning
mackey0225
2
1.3k
Featured
See All Featured
Building Applications with DynamoDB
mza
96
6.6k
Done Done
chrislema
185
16k
GraphQLの誤解/rethinking-graphql
sonatard
72
11k
Into the Great Unknown - MozCon
thekraken
40
2k
Building Flexible Design Systems
yeseniaperezcruz
329
39k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
KATA
mclloyd
32
14k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
Making the Leap to Tech Lead
cromwellryan
135
9.5k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
Agile that works and the tools we love
rasmusluckow
330
21k
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
`