Giter Site home page Giter Site logo

elementreejs / elementree Goto Github PK

View Code? Open in Web Editor NEW
12.0 12.0 0.0 2.85 MB

Create Web UIs with Proxy Observables and JS Template Literals

Home Page: https://npm.im/elementree

License: MIT License

JavaScript 100.00%
elementree front-end javascript javascript-framework mvvm mvvm-framework observable tiny

elementree's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar greenkeeper[bot] avatar mjstahl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

elementree's Issues

Routing is not updating app state with values captured from url path

When working on the todomvc example. Using route the route was updated, but the variable I had configured was not on the application state object.

const routes = {
  '#/:state': renderer
}
#/active //was expected to work, with a 'state' variable on app state set to the value of 'active'

Remove 'forceUpdate' from being exported

Removing forceUpdate for routing is more difficult. It cannot be removed until I figure out how to update app state without changing the route.

Maybe a special function that is attached to the appstate object that updates the value and re-renders the tree.

Possible memory leak when Element and children are not rendered

Right now, each components model is subscribed too. That way when it changes a render can start at the root and handle any changes.

Due to those changes, if an element is no longer rendered, its model is not unsubscribed from. This is probably a memory leak and needs to be rectified.

Probably with MutationObservers?

Idea: Bring '@elementree/location' into the main repo and add a 'routes' function

import { routes, merge, prepare } from 'https://unpkg.com/elementree'

// varargs function or array?
routes(
  '/items/:id',
  '/user/:name'
)

merge('body', prepare(...))

Application State would be populated a location and route property. Assuming an address of

.../items/1?sort=asc

Application State would look like:

location: '/items/1?sort=asc',
route: {
  path: {
    id: '1'
  },
  query: {
    sort: 'asc'
  },
  hash: ''
}

With any routes call or the arguments of routes is of length 0, the route property on Application State would be {}

Routing

// app.js
import { attach, routes } from 'elementree'

import dashboard from 'views/dashboard'
import user from 'views/user'

const routes = {
  initial: 'dashboard',
  'dashboard': dashboard,
  'user/:id': user
}

attach('body', routes)

routes accept a function or an object in which to create a Stated object. If a Function is passed, the routes function will turn it into:

{
  view: user,
  title: 'Sets the title of the browser tab'
  // any other properties
}
// views/user.js
import { couple, render } from 'elementree'

export const state = {
  // local states
}
export function template (model, app) {
  return render`
    <p>Hello User ${app.location.id}</p>
  `
}

export default couple(template, state) 

Routing

import { attach } from 'elementree'

import { hello, goodbye, notFound } from 'components/'

const routes = {
  '/hello/:name': hello,
  '/goodbye/:name': goodbye,
  '*': notFound
}

attach('body', routes)
// hello/hello.js
import { prepare, render } from 'elementree'

function hello ({ location }) {
  return render`
    <h1>Hello ${location.path.name}</h1>
  `
}

export default prepare(hello)

This adds a location property onto the AppState object passed to the template. The value of the location property is an object with two properties:

  • path, an object containing key/value pairs that match the variables defined in the route (:id)
  • query, an object containing key/value pairs parsed from the query string (anything past the ?)

The 'on-change' package currently does not work with classes / constructor functions

class Counter {
  constructor () {
    setInterval(this.onTick.bind(this), 1000)
  }
  onTick () {
    const now = new Date()
    this.hours = now.getHours()
    this.minutes = now.getMinutes()
    this.seconds = now.getSeconds()
  }
}

The update performed by onTick does not cause a re-render. This is probably because this refers to the instance and not the Proxy returned from the onchange call.

Double-rendering when the model changes state in the render function

function template (model, greeting) {
  if (greeting.toLowerCase() !== model.state) {
    model.to(model.actions.TOGGLE)
  }
  return render`
    <span>${model.value}</span>
  `
}

if the conditional is true, this template function will be called twice.

Not sure how this should be solved yet. And it may not be a huge deal for small trees, but any tree with enough leaves will eventually suffer a performance hit.

We could pass a render function or event down to each template.

Use requestAnimationFrame in __renderTree()?

Might be able to replace the following:

let rendering = false

function __renderTree () {
  if (rendering) { return }
  rendering = true
  rendering = !__merge(root, tree())
}

with:

function __renderTree() {
  window.requestAnimationFrame(() => {
    __merge(root, tree())
  })
}

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.