ManuelDeLeon / viewmodel

MVVM for Meteor
https://viewmodel.org
MIT License
205 stars 23 forks source link

Compare to React ? #118

Closed deanrad closed 8 years ago

deanrad commented 8 years ago

Manuel, I've started using ReactJS a bit, and notice how it shares a pretty close philosophy to ViewModel (which of course comes from MVVM). Could you maybe point out a few differences between Viewmodel and React?

Similar

Different

ManuelDeLeon commented 8 years ago

IMHO the most important similarities are:

The biggest difference I see is that ViewModel is declarative and React is imperative. With ViewModel you use or create bindings that encapsulate the functionality you want. The functionality can be anything from changing the element, wrapping it, adding/removing attributes, etc. While you can do the same with React, it wouldn't be in the spirit of the library.

And of course with ViewModel you need less lines of code to accomplish the same tasks =)

Regarding the <li>: that's because you have to use Blaze's #each. ViewModel sits on top of Blaze so it doesn't try to reinvent the wheel:

<ul>
  {{#each people}}
  <li>{{name}}</li>
  {{/each}}
</ul>

It does have the disadvantage that it gets a little verbose for small things like:

<template name="people">
  <ul>
    {{#each people}}
      {{> person}}
    {{/each}}
  </ul>
</template>
Template.people.viewmodel({
  selected: null
  people: function() {
    return People.find();
  }
}, 'people');
<template name="person">
  <li data-bind="text: name, click: select" ></li>
</template>
Template.person.viewmodel({
  select: function() {
    this.parent().selected(this._id());
  }
});

I'm working overtime on ViewModel v2 which will simplify things:

<template name="people">
  <ul>
    {{#each people}}
      <li {{b "text: this.name, click: selected(this._id)" }} ></li>
    {{/each}}
  </ul>
</template>
Template.people.viewmodel({
  selected: null,
  people: function() {
    return People.find();
  }
});

I'm trying to get the best of React and Angular (avoiding $scope hell of course).

Another thing I'm working on is simplifying the way you share state and functions between templates/components. I'll give you the option of sharing a vm property with all descendants, the instances of the same template, and even between non related view models.

The best use case for the last one is a search box on the heading. You will be able to define a set of properties (in our example it would only be 'searchText') and name them (say "search"). Then when you define the heading view model you would tell it to use the shared properties of "search". The same wherever else you have a list that depends on the search box. You would tell that view model to use the shared properties of "search". That way both view models are sharing the same property. No coupling, no spaghetti.

On the horizon I plan to create a time travel debugging tool for ViewModel. The hardest part here is to create a decent UI for it because the core functionality is actually pretty trivial when you use view models. All it has are properties/state and methods/actions, that's it. To create the time travel, all I need to do is throw the state and actions into an array of immutable objects.

Another thing on the horizon is a nicer view model explorer.

That was a lot so let me know if you have questions :)

deanrad commented 8 years ago

I love the idea of state and actions in an array of immutables. I think you are on track to have something that is of more practical convenience than either big framework, even if it seems not to have the 'brand recognition', but I swear by ViewModels, I've been doing them for years, and the syntax is to me much better for yours compared to React and all its chicanery.

ManuelDeLeon commented 8 years ago

Yeah, with view models people either swear by them or are like "meh, whatever".

btw, nice interview on OK Grow's podcast. I liked it very much.

deanrad commented 8 years ago

RE time travel - if you're thinking of recording mutations - do you think prior to that viewmodel could allow for mutating several fields at a time ? Then each mutation (even if against multiple fields at once) can go into history as all of its new values. I don't know if that exists, but I think it would be neat.

A slider is an interesting metaphor for position along a timeline - if people could drag left/right ?

deanrad commented 8 years ago

Ha, an old presentation i made on viewmodels 3 1/2 years ago ! https://docs.google.com/presentation/d/1mbwMZbqYdQCxfVih1Rry7iPl8waDEmsrR4z6Z5UrK14/edit?usp=sharing

deanrad commented 8 years ago

Are there any production projects you can say you've used this on ? Even if anonymous - anything about them you could share for what level of scale you've gotten to with viewmodels alone ?

ManuelDeLeon commented 8 years ago

I've already done something like it (recording and playback with a slider). Check out https://www.meteorcasts.net/ep/6

I'll check out your presentation ... On Oct 10, 2015 11:39 PM, "Dean Radcliffe" notifications@github.com wrote:

Ha, an old presentation i made on viewmodels 3 1/2 years ago ! https://docs.google.com/presentation/d/1mbwMZbqYdQCxfVih1Rry7iPl8waDEmsrR4z6Z5UrK14/edit?usp=sharing

— Reply to this email directly or view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/118#issuecomment-147160200 .

ManuelDeLeon commented 8 years ago

People don't mention what they're working on. The only one i know of is https://abesea.com/ and only because I keep in touch with the main dev of that site. On Oct 10, 2015 11:45 PM, "Manuel De Leon" manueldeleon@gmail.com wrote:

I've already done something like it (recording and playback with a slider). Check out https://www.meteorcasts.net/ep/6

I'll check out your presentation ... On Oct 10, 2015 11:39 PM, "Dean Radcliffe" notifications@github.com wrote:

Ha, an old presentation i made on viewmodels 3 1/2 years ago ! https://docs.google.com/presentation/d/1mbwMZbqYdQCxfVih1Rry7iPl8waDEmsrR4z6Z5UrK14/edit?usp=sharing

— Reply to this email directly or view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/118#issuecomment-147160200 .

ManuelDeLeon commented 8 years ago

@deanius Check out v2: https://viewmodelv2.meteor.com/docs/ The library is pretty much finished but I still need to add a few things to the documentation. I also want to redo the phone book example (if I don't burn out by then).

Here are a few things you might find interesting: https://viewmodelv2.meteor.com/docs/viewmodels#share https://viewmodelv2.meteor.com/docs/viewmodels#mixin https://viewmodelv2.meteor.com/docs/misc#controls https://viewmodelv2.meteor.com/docs/misc#hotcodepush https://viewmodelv2.meteor.com/docs/misc#stateonurl

deanrad commented 8 years ago

Thanks for the update - I'll check it out !

deanrad commented 8 years ago

There is no site deployed at this address.

ManuelDeLeon commented 8 years ago

meteor deployed sites are down. I'll see if I can put it somewhere else.

ManuelDeLeon commented 8 years ago

And of course they come back up as soon as I posted that. Try again :)

deanrad commented 8 years ago

Thanks - just sang your praises on the meteor forums. Closing issue for now.

ManuelDeLeon commented 8 years ago

You almost made me cry =) On Nov 18, 2015 7:56 PM, "Dean Radcliffe" notifications@github.com wrote:

Closed #118 https://github.com/ManuelDeLeon/viewmodel/issues/118.

— Reply to this email directly or view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/118#event-468727403.

deanrad commented 8 years ago

Just curious what you're doing now that free meteor hosting is gone?

Also, I'm working in React now, and this is my fave React/Redux forms impl at the moment: http://redux-form.com/4.2.0/#/examples/synchronous-validation?_k=7v8sbx

fvpDev commented 8 years ago

https://viewmodel.org/ep/7

ManuelDeLeon commented 8 years ago

Hey Dean, good to see you.

I'm just hosting the documentation on an Azure VM (https://viewmodel.org). It's a (very) low traffic site so I can get by with the bare minimum. I added a paid/donate videos section that I use to pay for the hosting (and have a beer once in a while).

Can you do me a favor and make a working repo out of that synchronous validation example in redux-form? I want to see it in action, not just the "cleaned up for clarity" version. I couldn't find it on their GitHub repo. Let's console.log the form's data on submit.

ViewModel for react is coming along quite nicely. I'm very happy with the API (syntactic sugar mostly):

./Person.jsx

Person({
  name: '',
  greeting() {
    return "Hello " + this.name();
  },
  render() {
    <div>
      <input b="value: name" />
      <p b="text: greeting" />
    </div>
  }
})

./index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Person } from './Person.jsx';

ReactDOM.render(<Person />, document.getElementById('root'));