Are people using the knockout.model plugin?

998 views
Skip to first unread message

Stacey Thornton

unread,
Aug 23, 2011, 9:57:06 AM8/23/11
to KnockoutJS
I discovered thelinuxlich's knockout.model plugin recently (
https://github.com/thelinuxlich/knockout.model ) and was wondering if
anyone is using it in production, and if so what their experiences
with it so far have been. I am not fluent in coffee script and trying
to read through it I am having difficulty understanding a bit of what
it does, but comparing it to backbonejs, it seems to be a similar
approach. Can anyone comment? So far I've found this person's other
knockout related files on github to be very useful so I have no doubt
that this one is as well.

Ω Alisson

unread,
Aug 23, 2011, 10:03:22 AM8/23/11
to knock...@googlegroups.com
Hi Stacey, you are right. Knockout.model was inspired by Backbone stuff, actually I use it heavily in production(there are some apps here using 10+ models), but everything's intranet :(
Many people ask me about some concrete examples but I just can't find time at the moment to spend on it, but if you have any doubts just ask :)

Stacey Thornton

unread,
Aug 23, 2011, 10:21:18 AM8/23/11
to KnockoutJS
I do not have doubts. Just confusion.

I think my primary question is 'What does this solve?' Please do not
take that the wrong way, I am not saying it does not solve something,
I am just simply stating I do not understand what it solves.

Ω Alisson

unread,
Aug 23, 2011, 10:43:32 AM8/23/11
to knock...@googlegroups.com
For me it's an easy way to reuse js models through all applications here and cut many lines to keep things DRY. Once you define your models, you can set its properties(observable or not) with an object(like backbone and also detecting html entities and unescaping them), convert it to JS/JSON excluding some properties, define what you want to send to the server, define REST urls with parameter syntax like Sinatra, default values on instantiation and many other things.

You could live without it? For sure! It's just a matter of code reuse and conciseness, like these:

# VM = viewmodel

VM.loadGroups = (callback) ->
    Group.index {questionnaire_id: VM.questionnaire.id()}, (response) ->
        VM.questionnaire.groups Group.createCollection response.data
        callback() if typeof callback is "function"

VM.deleteQuestionnaire = (callback) ->
    VM.questionnaire.destroy (data) ->
       if data.status is "SUCCESS"
           do_something()
       alert data.message.unescapeHtml()
       callback() if typeof callback is "function"

VM.saveQuestion = ->
    VM.question.save (response) ->
       if response.status is "SUCCESS"
           if VM.question.isNew()
               do_something_about_this_new_record()
           else
               do_something_about_this_updated_record()
           VM.question.clear()

VM.back = ->
    VM.questionnaire.clear()
    Questionnaire.killAllRequests()

Scott Messinger

unread,
Aug 26, 2011, 3:48:22 PM8/26/11
to knock...@googlegroups.com
I looked at the plugin in a while ago and I really like the idea. I like the simplicity and clarity of Backbone and I'm glad thelinuxlich created a plugin around it. I ended up not using it for my project because 
(a) it was in coffeescript
(b) I didn't see how to use the mapping plugin with it.
(c) The getting/setting of attributes confused me and felt unnecessary, given how KO works

I think there's a need for a model plugin with KO and I'm glad people are exploring how to do it! I look forward to seeing how the plugin develops.

thelinuxlich

unread,
Aug 26, 2011, 4:07:17 PM8/26/11
to KnockoutJS
Thank you for looking after this plugin, Scott. Let me clear your
doubts:

a) You don't need coffeescript to use the plugin, just include the
knockout.model js file and you're ready to go! The thing is, the
plugin utilizes coffeescript object inheritance, which you can write
by yourself in javascript like this template:

(function() {

// object inheritante ugly stuff
var __hasProp = Object.prototype.hasOwnProperty, __extends =
function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key))
child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};

// the model
this.Questionnaire = (function() {
__extends(Questionnaire, KnockoutModel);

// RESTful urls
Questionnaire.__urls = {
"index": "http://" + window.location.host + "/aplic/
editor_questionarios/get/busca_questionarios.php",
};

// Parameters we don't want to send to the server
Questionnaire.__transientParameters = ["someTempValue"];

// attributes declaration
function Questionnaire() {
this.id = ko.observable("");
this.title = ko.observable("");
this.someTempValue = ko.observable("foo");
Questionnaire.__super__.constructor.call(this);
}

return Questionnaire;
})();
}).call(this);

b) I don't use the mapping plugin so I never thought about some kind
of bridge, but you can always set the model instance values with a
valid js object:
questionnaire.set({id: 2, title: "test"});

c) model.get() is mostly used internally, since you can have
observable and non-observable attributes, but I think model.set() is
really useful, instead of passing a chain setting observable(or non-
observable) values you can just pass an object to feed the model.

studgeek (David Rees)

unread,
Sep 13, 2011, 4:13:18 PM9/13/11
to KnockoutJS
Yet another interesting plugin:). I added it to the new wiki Plugins
list - https://github.com/SteveSanderson/knockout/wiki/Plugins.

On Aug 23, 9:57 am, Stacey Thornton <stacey.cielia.l...@gmail.com>
wrote:
> I discovered thelinuxlich's knockout.model plugin recently (https://github.com/thelinuxlich/knockout.model) and was wondering if

Greg Tomei

unread,
Oct 5, 2011, 7:28:36 AM10/5/11
to KnockoutJS
Hi all,

I like the look of this plugin and have been looking into using it at
work. I am having trouble getting some methods to work and I am
wondering which version of the plugin works with which version(s) of
Knockout? I tried the KO 1.3 beta and hit a bug and went back to KO
1.2.1. Is 1.2.1 of KO correct to work with HEAD of the plugin?

Thanks,
Greg

Ω Alisson

unread,
Oct 5, 2011, 8:25:36 AM10/5/11
to knock...@googlegroups.com
I'm using it with KO 1.3 without issues, what's the bug?

joelparkerhenderson

unread,
Oct 18, 2011, 5:29:41 PM10/18/11
to KnockoutJS
We're using it in production. It's well written and dependable.

Kaleb Sturgill

unread,
Oct 20, 2011, 3:50:45 PM10/20/11
to knock...@googlegroups.com
I am trying to figure out how to use it and made a jsFiddle. Is this how you would use it without CoffeeScript?



Ω Alisson

unread,
Oct 20, 2011, 4:04:41 PM10/20/11
to knock...@googlegroups.com
Exactly!

Kaleb Sturgill

unread,
Oct 21, 2011, 10:16:44 AM10/21/11
to knock...@googlegroups.com
Is this able to do model nesting? 

See the example I am trying to get working:
http://jsfiddle.net/kmanpro/LxDxj/

Ω Alisson

unread,
Oct 21, 2011, 10:23:23 AM10/21/11
to knock...@googlegroups.com
No, you should have separate class files(myothermodel.js and mymodel.js)

thelinuxlich

unread,
Nov 7, 2011, 7:15:30 AM11/7/11
to KnockoutJS
Latest additions:

Override the __afterHooks attribute with a object containing callbacks
to run after a route is invoked
obj.start_transaction() - Disconnects the model of subscribers
temporarily
obj.commit() - Reconnects the model with its subscribers and notifies
them
obj.backup() - Stores all model values in a temporary internal objects
obj.restore() - Restores all model values saved with obj.backup()

Muneeb

unread,
Dec 16, 2011, 4:25:05 AM12/16/11
to KnockoutJS
It's working pretty neatly with the 1.3 version.

Muneeb

On Nov 7, 5:15 pm, thelinuxlich <thelinuxl...@gmail.com> wrote:
> Latest additions:
>
> Override the __afterHooks attribute with a object containing callbacks
> to run after a route is invoked
> obj.start_transaction() - Disconnects the model of subscribers
> temporarily
> obj.commit() - Reconnects the model with its subscribers and notifies
> them
> obj.backup() - Stores all model values in a temporary internal objects
> obj.restore() - Restores all model values saved with obj.backup()
>

thelinuxlich

unread,
Aug 23, 2012, 5:24:29 PM8/23/12
to knock...@googlegroups.com
Just updated the plugin to remove coffeescript dependence!
Reply all
Reply to author
Forward
0 new messages