I searching the best way to objects in AngularJS. The goal is to use it like this model = new Model 'wowwowwow'
So I started with this approach:
app.factory 'Model', ->
(blah) ->
Model =
blah: ''
foo: []
Model.blah = blah
Model
Then I wanted to move constructor actions in separate function that would not be accessible from class instance:
app.factory 'Model', ->
constructor = (blah) ->
@blah = blah
@
(blah) ->
constructor.call
blah: ''
foo: []
Finally I wanted to use goals of coffeescript and tried to solve my task in kinda obvious way:
app.factory 'Model', ->
class Model
blah: ''
foo: []
constructor: (blah) ->
@blah = blah
(blah) ->
new Model blah
But in this last case doesn't work as needed:
var1 = new Model 'test'
var1.foo.push 'blah'
var2 = new Model 'asdasd'
console.log var2.foo
the problem here is that var2 after creation will have same values as var1 (they are linked in fact).
Here is the plunk for this problem.
So the questions are:
What is wrong with my 3rd approach?
How can I change 3rd approach to make it work with coffeescript's OOP features.
Are any better approaches for my task?