thomas
thomas

Reputation: 155

Spine.js Dynamic Models // Coffeescript Dynamic Class Creation

Ladies & Gentlemen,

Short Version: Trying to dynamically create a Spine Model Class. Basically I get a JSON from the server telling me what the model name is and what it´s attributes are and then I´ll try to create a class derived from spine.model.

As Spine.js uses a Coffeescript class that is configured before being instantiated I wanted to create one dynamically - Which works, only some of the inherited functionality does not.

Problem: While inherited functions work, fetch doesn´t seem to work and there seems no way to set the class name.

Code: http://jsfiddle.net/thomasf1/eqksA/5/

It´s fascinating how far you can push Coffeescript. Thanks in advance for any clue to solving this advanced riddle.

Upvotes: 0

Views: 417

Answers (1)

Sandro
Sandro

Reputation: 4771

I'm not exactly sure how Spine.js works, but looking through your code I noticed fetch is a class method of CDModel, and that's how you use it in the first test where you fetch 'static message,' but for the dynamic class you're trying to use fetch as an instance method.

I changed the name to DynamicClass for my own clarity, but I did the following:

// The class name is DynamicClass
DynamicClass = gen ("Testclass")
// ... more code ...
DynamicClass.bind("refresh change", (item) -> $("#element2").html DynamicClass.first().message )

params = type: 'POST', data: 'json=[{"message":"Dynamic msg"},{"message":"another message"}]'
DynamicClass.fetch(params)

I noticed this only works when you comment out the call to the first test though, but not being familiar with Spine.js I'm not entirely sure why this occurs.

I hope this helps. Sandro

Upvotes: 1

Related Questions