This may be very beginner question, or maybe I am using too many things at once. I am trying to use KnockoutJS with the Revealing Module Pattern and trying to write CoffeeScript. I have put my model in a separate function. Here is the code:
myapp =(->
person =->
firstname = ko.observable "k"
lastname = ko.observable "d"
firstname:firstname,
lastname:lastname
person : person
)()
$(->
ko.applyBindings(new myapp.person())
@
)
Now this is creating JavaScript using workbench in Visual Studio like this:
(function() {
var myapp;
myapp = (function() {
var person;
person = function() {
var firstname, lastname;
firstname = ko.observable("k");
lastname = ko.observable("d");
return {
firstname: firstname,
lastname: lastname
};
};
return {
person: person
};
})();
$(function() {
ko.applyBindings(new myapp.person());
return this;
});
}).call(this);
Now this is giving error that it could now found element to bound. Here is the simple HTML text:
<p>
Firstname<span data-bind="text: myapp.person().firstname"></span>
Lastname<span data-bind="text: myapp.person().lastname">
</span>
</p>
If I create normal JavaScript without self called function it working fine. Here is the function:
myapp = (function () {
var person;
person = function () {
var firstname, lastname;
firstname = ko.observable("k");
lastname = ko.observable("j");
return {
firstname : firstname,
lastname : lastname
}
}
return {
person: person
}
})();
$(function () {
ko.applyBindings(new myapp.person());
})
Can anyone explain what is going wrong here? Or is there another way to write CoffeeScript to use KnockoutJS?
Please comment if further details are needed.