Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Context</strong><br> I have this raw data:</p> <pre><code>data = name: first: 'alan' last: 'smith' age: 18 address: [ { name: 'work' street: '1600 amphitheatre parkway' city: 'mountain view' zip: 'CA 94043' },{ name: 'home' street: '1 infinite loop' city: 'cupertino' zip: 'CA 95014' }] </code></pre> <p>And want to create a structure to manipulate my data like this :</p> <pre><code>p = New Person(data) console.log """ #{p} # alan smith (18), 2 address lines #{p.name}, # alan smith #{p.name.first}, # alan #{p.address} # 2 address lines #{p.address.work} # 1600 amphitheatre parkway, mountain view, CA 94043 #{p.address.work.street} # 1600 amphitheatre parkway """ </code></pre> <p>Additionally, I want to be able to apply custom methods to any member. For instance, assuming toCap() is a method that capitalises each word of a String :</p> <pre><code>console.log """ #{p.name.toCap()}, # Alan Smith #{p.name.first.toCap()} # Alan #{p.address.work.toCap()} # 1600 Amphitheatre Parkway, Moutain View, CA 94043 #{p.address.work.street.toCap()} # 1600 Amphitheatre Parkway """ </code></pre> <hr> <p><strong>Solution</strong> (see this <a href="http://plnkr.co/edit/ovwj7z" rel="nofollow" title="See Plunk Here">Plunk</a> for the full code) </p> <ul> <li><p>use nested classes </p> <pre><code>class Person constructor: (data) -&gt; @name = new Name(data.name) @address = new AddressList(data.address) </code></pre></li> <li><p>create members dynamically</p> <pre><code>class AddressList constructor: (list) -&gt; @[addr.name] = new Address(addr) for addr in list </code></pre></li> <li><p>wrap your properties or use base classes rather than extending native objects</p> <pre><code>class StringProperty constructor: (@_value) -&gt; toString: =&gt; @_value toCap: (remainingToLower=false) =&gt; _words = @_value.split ' ' (@_toCap(w,remainingToLower) for w in _words).join ' ' _toCap : (s, r) -&gt; s[0].toUpperCase() + if r then s[1..-1].toLowerCase() else s[1..-1] </code></pre> <p>... and use them directly ...</p> <pre><code>class Name constructor: (name) -&gt; @first = new StringProperty(name.first) @last = new StringProperty(name.last) toString: =&gt; "#{@first} #{@last}" toCap: =&gt; "#{@first.toCap()} #{@last.toCap()}" </code></pre> <p>... or create members dynamically :</p> <pre><code>@[k] = new StringProperty(data[k]) for k of data when k in Address.fields </code></pre></li> <li><p>don't forget to override toString() as above</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload