Last active
August 29, 2015 14:06
-
-
Save jtremback/1eaece0807fa3fe02262 to your computer and use it in GitHub Desktop.
Meteor attempts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 'use strict'; | |
// An app that has items and stories. | |
// Items have properties: name, weight, description. | |
// Stories have properties: title, content, item | |
// Stories contain a widget for their item | |
// The add item screen shows this widget, live updating with a binding to the form. | |
// The same widget template code being used on both screens. | |
// Screens: story detail, add/edit story, add/edit item | |
var counterA = 0; | |
var counterB = 0; | |
var counterC = 0; | |
Items = new Meteor.Collection('items'); | |
Stories = new Meteor.Collection('stories'); | |
Cache = new Meteor.Collection(null); | |
if (Meteor.isClient) { | |
Router.map(function() { | |
this.route('items_edit', { | |
path: '/items/:id/edit', | |
template: 'items_edit', | |
onRun: function () { | |
Session.set('item_id', this.params.id); | |
}, | |
waitOn: function() { | |
return Meteor.subscribe('items', this.params.id); | |
}, | |
data: function() { | |
var item = Items.find({ _id: this.params.id }).fetch()[0]; | |
var tempItem = Cache.find({ _id: this.params.id }).fetch()[0]; | |
// console.log(++counter, item, tempItem) // Pretty cool | |
if (item && !tempItem) { | |
Cache.insert(item); | |
} | |
return { | |
item: item, | |
tempItem: tempItem | |
}; | |
} | |
}); | |
this.route('stories_detail', { | |
path: '/stories/:id', | |
template: 'stories_detail', | |
waitOn: function() { | |
return Meteor.subscribe('story', this.params.id); | |
}, | |
data: function() { | |
console.log('data', ++counterA, Stories.find().fetch(), Items.find().fetch()) | |
return Stories.find(); | |
}, | |
onBeforeAction: function() { | |
if (this.data().fetch().length) { | |
// debugger | |
var story = this.data().fetch()[0]; | |
this.subscribe('item', story.item_id).wait(); | |
console.log('onBeforeAction', ++counterB, Stories.find().fetch(), Items.find().fetch()) | |
} | |
} | |
}); | |
this.route('items_new', { | |
path: '/items/new', | |
template: 'items_edit', | |
onRun: function () { | |
Session.set('item_id', Cache.insert({})); | |
}, | |
data: function() { | |
var tempItem = Cache.find(Session.get('item_id')).fetch()[0]; | |
return { | |
tempItem: tempItem | |
}; | |
} | |
}); | |
}); | |
var formBind = function (event, key, events) { | |
events[event + ' #' + key] = function (e) { | |
var modifier = {}; | |
modifier.$set = {}; | |
modifier.$set[key] = e.target.value; | |
Cache.update({ _id: Session.get('item_id') }, modifier); | |
}; | |
}; | |
var item_form_events = {}; | |
formBind('input', 'name', item_form_events); | |
formBind('input', 'description', item_form_events); | |
formBind('input', 'weight', item_form_events); | |
item_form_events['submit form'] = function(e) { | |
e.preventDefault(); | |
var item_id = Session.get('item_id'); | |
var tempItem = Cache.find({ _id: item_id }).fetch()[0]; | |
Items.upsert(item_id, tempItem); | |
Router.go('/items/' + item_id + '/edit'); | |
}; | |
Template.item_form.events(item_form_events); | |
} | |
if (Meteor.isServer) { | |
Meteor.startup(function () { | |
console.log('startup', Items.find().fetch(), Stories.find().fetch()); | |
if (Items.find().count() === 0) { | |
Items.insert({ | |
name: 'Apple', | |
description: 'Round fruit.', | |
weight: '6oz', | |
_id: 'QjaQtXFzAmuKp4kME' | |
}); | |
} | |
if (Stories.find().count() === 0) { | |
Stories.insert({ | |
title: 'Fruit Story', | |
content: 'If she shun\'d The white stovepipe doubled in two, its large hole as the light of the World, a part of the hole and glassy--a kind of semi-putrid congealed Deareling of the shuddered, and made. With the deepening of the great carboys, and last enchaunted him ever so little to shook perceptibly, but when it is torn down the electric torch I had seen. The dampness was unspeakably shovel the birds nearly unnerved at last year the blindrical; like along the him, at his perills was less fascinates me, and house\'s terraced garden, and saw that charnel gulf and horrible fungi which it severed what its strange fungi had provided. The dampness was less grayish thinking black earth in two, its for a time came front of the many tears with Drugs or Minerals, That weakens Motion, Would corrosive contents of inner earth\'s nethermost terrors had provided. Then courage returned up the struck something softer the fungi had provided.', | |
item_id: 'QjaQtXFzAmuKp4kME', | |
_id: 'ta8LebqkFcXm7ycsz' | |
}); | |
} | |
}); | |
Meteor.publish('story', function(_id) { | |
return Stories.find({ _id: _id }); | |
}); | |
Meteor.publish('item', function(item_id) { | |
return Items.find({ item_id: item_id }); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template name="items_edit"> | |
{{> item_form tempItem}} | |
{{> item_widget tempItem}} | |
</template> | |
<template name="stories_detail"> | |
<div class="container"> | |
<section class="title-box"> | |
<h2>title: {{story.title}}</h2> | |
</section> | |
<div class="row"> | |
<div class="col-sm-9"> | |
{{story.content}} | |
</div> | |
<div class="col-sm-3"> | |
{{> item_widget item}} | |
</div> | |
</div> | |
</div> | |
</template> | |
<template name="item_form"> | |
<form class="form-horizontal" role="form"> | |
<div class="form-group"> | |
<label for="name" class="col-sm-2 control-label">Name</label> | |
<div class="col-sm-10"> | |
<input type="text" class="form-control" name="name" id="name" value="{{name}}"> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="description" class="col-sm-2 control-label">Description</label> | |
<div class="col-sm-10"> | |
<textarea class="form-control" name="description" id="description" value="{{description}}"></textarea> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="weight" class="col-sm-2 control-label">Weight</label> | |
<div class="col-sm-10"> | |
<textarea class="form-control" name="weight" id="weight" value="{{weight}}"></textarea> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-offset-2 col-sm-10"> | |
<button type="submit" class="btn btn-default">Save Item</button> | |
<button class="btn btn-default delete">Delete Item</button> | |
<button class="btn btn-default cancel">Cancel</button> | |
</div> | |
</div> | |
</form> | |
</template> | |
<template name="item_widget"> | |
<div>Name: {{name}}</div> | |
<div>Description: {{description}}</div> | |
<div>Weight: {{weight}}</div> | |
</template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment