Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TemplateController w/o TemplateController :) #30

Closed
comerc opened this issue May 5, 2016 · 5 comments
Closed

TemplateController w/o TemplateController :) #30

comerc opened this issue May 5, 2016 · 5 comments
Assignees

Comments

@comerc
Copy link
Contributor

comerc commented May 5, 2016

Template2:

Template.hello.onCreated(function helloOnCreated() {
  this.schema(
    new SimpleSchema({
      test: { type: String }
    })
  );
  this.states({ counter: 777 });
  this.events({
    'click button'() {
      // increment the counter when button is clicked
      this.state.counter++;
    },
  });
});
<body>
  <h1>Welcome to Meteor!</h1>

  {{> hello test='123'}}
</body>

<template name="hello">
  {{props.test}}
  <button>Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>
</template>
@comerc
Copy link
Contributor Author

comerc commented May 8, 2016

Released!

key features

  • compatible with Blaze.Template
  • minimum changes for migration your great project to template2
  • one time declaration of variables to model - inside input fields attribute
  • validate input data and get form data without excess coding
  • support of model type as SimpleSchema
  • may be extended for support any other model type (Astronomy etc.)
head
  title simple

body
  +demo(test='123')

template(name="demo")
  div props: 
    = props.test
  div 
    a.node(href="#") NEW
  form
    input(type="text" value-bind="fieldHead|throttle:500") 
    = state.fieldHead
    input(type="text" value-bind="fieldBody|debounce:500") 
    = state.fieldBody
    button(type="submit") SUBMIT
    button#reset RESET
    = state.submitMessage
  = state.errorMessages
  ul
    +each node in nodes
      li 
        a.node(href="#" data-node-id=node._id)  
          = node.fieldHead  
          | / 
          = node.fieldBody
Template.demo.onCreated ->
  @propsSchema new SimpleSchema(test: type: String)
  @modelSchema Nodes.simpleSchema()
  @states
    nodeId: false
    submitMessage: ''
  @helpers
    nodes: ->
      Nodes.find()
  @events
    'click a.node': (e) ->
      e.preventDefault()
      @state.nodeId = $(e.target).data('node-id') or false
    'submit form': (e) ->
      e.preventDefault()
      @viewDoc (error, doc) ->
        return if error
        # save data
        if @state.nodeId
          Nodes.update @state.nodeId, $set: doc,
            => @state.submitMessage = 'updated'
        else
          @state.nodeId = Nodes.insert doc,
            => @state.submitMessage = 'inserted'

# old school of declaration with context of Template.instance()
Template.demo.eventsByInstance
  'click #reset': (e) ->
    e.preventDefault()
    @modelDoc false
    @state.submitMessage = ''

Template.demo.onRendered ->
  @modelMap() # magic here :)
  @autorun =>
    if @state.nodeId
      @modelDoc Nodes.findOne @state.nodeId
    else
      @modelDoc false
    @state.submitMessage = ''
@Nodes = new (Mongo.Collection)('nodes')

@NodeSchema = new SimpleSchema
  fieldHead:
    type: String
    label: 'My Head'
    max: 3
    defaultValue: '111'
  fieldBody:
    type: String
    label: 'My Body'
    min: 3
    defaultValue: '777'

Nodes.attachSchema(NodeSchema)

@comerc comerc closed this as completed May 8, 2016
@comerc
Copy link
Contributor Author

comerc commented May 12, 2016

I want continue our coloboration! :)

@comerc comerc reopened this May 12, 2016
@DominikGuzei
Copy link
Member

@comerc it's fine if you just use or create another MVVM package but this package intentionally tries not to tap into that water and stay as close to the official Blaze guide as possible, while supporting best practices and clean code. IMO splitting up view code into separate different styles and files or introducing the overloaded term "model" there is not at all best practices nor would i recommend this.

@comerc
Copy link
Contributor Author

comerc commented May 14, 2016

OK, core - wo MVVM. And mixin may be separated.

@DominikGuzei
Copy link
Member

Thanks for the core implementation @comerc, i opened issue #33 for adding this into the lib 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants