coffeescript Class
Asked Answered
S

1

2

When I replace new Gallery with new Kinetic.Stage the code works properly When I work with the dirived class it does not work.

Why is the kind of deriving Gallery from Kinetic.Stage wrong ?

width = window.innerWidth   || document.documentElement.clientWidth || document.body.clientWidth || 0
height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0


class Gallery extends Kinetic.Stage
  constructor: (config) -> 
      super(config)



window.onload = -> 
  list_of_photos = jQuery('#_image img')
  x_pos = width/4
  y_pos = height/4
  stage = new Gallery
                container: "gallery_container"
                width: width
                height: height
  images_layer = new Kinetic.Layer()


  for image in list_of_photos
    imageObj = new Image()
    imageObj.src = image.src
    x_pos = x_pos + 100
    y_pos = y_pos + 10
    ori = new Kinetic.Image
                x: x_pos 
                y: y_pos
                image: imageObj
                draggable: true
                width: 200
                height: 200
    images_layer.add ori
  stage.add images_layer
Sphinx answered 25/1, 2013 at 21:10 Comment(3)
KineticJS is a Javascript libSphinx
The error is TypeError: stage.add is not a function [Bei diesem Fehler anhalten] return stage.add(images_layer);Sphinx
What I wonder is that the method stage.add is not present for the gallery class but for Kinetic.Stage.Sphinx
D
5

Problem - Kineticjs Objects are not "compatible" with coffeescript classes.

Solving - You have to use other way of calling "super"

class Gallery extends Kinetic.Stage
    constructor : (config) ->
        Kinetic.Stage.call(@, config)

Example here : http://jsfiddle.net/lavrton/w2EQD/4/

UPD: I found that this problem exists only for "constructor" method. Ok for others:

class Gallery extends Kinetic.Stage
  constructor: (config) -> 
    Kinetic.Stage.call(@, config)
  add : (item) ->
    console.log(item)
    super item
Disputable answered 26/1, 2013 at 11:21 Comment(2)
OK super, thanks for this aid (as a coffee and js newbee I had no chance to find that). This brings me to the question if there are other non compatible libs out there. Is it good to use KineticJS with Coffeescript or is it better to search for an alternative. Whats your opinion ? Tahnks again ErhardSphinx
Using KineticJS and Coffescript is good. I think that large part of libs are not natively compatible with coffeescript classes. I am using kineticjs with coffee in my projects. And I'm not sure whether you need to "extend" native lib's objects. Work around : procedural style or create Gallery class and add Stage as member.Disputable

© 2022 - 2024 — McMap. All rights reserved.