-
-
Save m9dfukc/3312572 to your computer and use it in GitHub Desktop.
SpineJS User's Name helper + showing off getters/setters in Spine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecord::Schema.define(:version => 00000000000000) do | |
create_table "users", :force => true do |t| | |
t.string "username", :default => "", :null => false | |
t.string "email", :default => "", :null => false | |
t.string "encrypted_password", :default => "", :null => false | |
# Devise fluff here... | |
t.string "first_name", :default => "", :null => false | |
t.string "last_name", :default => "", :null => false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.Name = class Name | |
constructor: (user, name_parts) -> | |
@user = user | |
_full = name_parts.map (w) -> w.capitalize() | |
_first = _full[0] | |
_last = _full[_full.length-1] | |
Object.defineProperty @, "first", | |
get: -> _first | |
set: (val) -> | |
_first = val.trim().capitalize() | |
user.first_name = _first | |
Object.defineProperty @, "last", | |
get: -> _last | |
set: (val) -> | |
_last = val.trim().capitalize() | |
user.last_name = _last | |
Object.defineProperty @, "full", | |
get: -> [_first, _last].join(" ") | |
set: (val) -> | |
_full = val.underscore().split("_").map (w) -> w.trim().capitalize() | |
@first = _full[0] | |
@last = _full[_full.length-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User extends Spine.Model | |
@configure 'User', 'email', 'username', 'first_name', 'last_name' | |
@extend Spine.Model.Ajax | |
constructor: (attributes) -> | |
super attributes | |
@name = new Name(@, [@first_name, @last_name]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> user = new User({first_name: "Ryan", last_name: "Lewis"}); | |
< User | |
> user.first_name; | |
< "Ryan" | |
> user.last_name; | |
< "Lewis" | |
> user.name.first; | |
< "Ryan" | |
> user.name.first = "Randy" | |
< "Randy" | |
> user.name.first; | |
< "Randy" | |
> user.name.full; | |
< "Randy Lewis" | |
> user.name.full = "Janis Joplin"; | |
< "Janis Joplin" | |
> user.name.first; | |
< "Janis" | |
> user.name.last; | |
< "Joplin" | |
> user.name.full; | |
< "Janis Joplin" | |
> user.first_name; | |
< "Janis" | |
> user.last_name; | |
< "Joplin" | |
> console.log("Cool, it just works!); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment