Giter Site home page Giter Site logo

Handle ES6 classes about marty HOT 6 CLOSED

martyjs avatar martyjs commented on July 28, 2024
Handle ES6 classes

from marty.

Comments (6)

jhollingworth avatar jhollingworth commented on July 28, 2024

@KidkArolis @oliverwoodings would love to hear your thoughts on this

from marty.

jhollingworth avatar jhollingworth commented on July 28, 2024

In v0.9 branch

from marty.

dariocravero avatar dariocravero commented on July 28, 2024

@jhollingworth could you provide an example of getting some value from a Marty.Store from a Marty.Component?
I've been going through the source and trying to make it work for a couple of hours now and can't seem to discover what am I missing. I'm essentially doing the same thing that's being proposed on the examples:

import Marty from 'marty'
import React from 'react'

export class BlockStore extends Marty.Store {
  constructor(options) {
    super(options)

    this.state = {
      1: {
        data: {
          some: 'data'
        }
      }
    }
  }

  getData(id) {
    return this.state[id].data;
  }
}

export default class Block extends Marty.Component {
  constructor(props, context) {
    super(props, context)

    this.listenTo = BlockStore
  }

  getState() {
    return {
      data: BlockStore.getData(this.props.id)
    }
  }

  render() {
    return (
      <div>
        Block Marty!
        {JSON.stringify(this.state.data)}
      </div>
    )
  }
}

But a call to BlockStore.getData(this.props.id) fails because getData is now an instance method of BlockStore and not a class one.

After reading #153 I have a feeling I'm missing something too.

Could you hint me in the right direction? :)
Thanks,
Darío

from marty.

jhollingworth avatar jhollingworth commented on July 28, 2024

Sorry, documentation is next on my list :)

We have a new top level API Marty.register(clazz) which will register the class into the internal container and return a new instance of the class. So given your example you would do

class BlockStore extends Marty.Store {
  constructor(options) {
    super(options)

    this.state = {
      1: {
        data: {
          some: 'data'
        }
      }
    }
  }

  getData(id) {
    return this.state[id].data;
  }
}

exports Marty.register(BlockStore)

Marty.createStore is now doing this underneath except we convert the object literal into class before passing it to register.

I'm not entirely happy with this solution but I can't think of a nicer way of doing it... One thing I'm considering is having a browserify transform which automatically wraps anything you export with, e.g. the above would become:

var BlockStore = Marty.register(class _BlockStore { ... })
export BlockStore;

from marty.

jhollingworth avatar jhollingworth commented on July 28, 2024

Alt does something almost identical

from marty.

dariocravero avatar dariocravero commented on July 28, 2024

Class! :) Thanks! The funny thing is that within all that trying I had a call to Marty.register(BlockStore) because from #153 it was clear that register was indeed needed but I wasn't storing that result anywhere :) (thought it was more like a "register this thing here and it will do some magic" kind of thing :), luckily it's not like that. Will chime in with some docs these days too.

from marty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.