Skip to content

Instantly share code, notes, and snippets.

@j201
Created June 7, 2014 02:25
Show Gist options
  • Save j201/be461eda905889516575 to your computer and use it in GitHub Desktop.
Save j201/be461eda905889516575 to your computer and use it in GitHub Desktop.
var marked = require('marked');
var highlight = require('my-code-highlighter');
exports.render = function(el, text) {
el.innerHTML = marked(text, { highlight: highlight });
};
var ajax = require('imaginary-ajax-lib');
var postsURL = "example.com/posts"; // Assuming a constant location. This could be passed as an argument to exports.fetch instead
// Returns a promise of a post array
exports.fetch = function() {
return ajax.get(postsURL)
.then(function(posts) {
// process posts if you want
return posts;
})
};
// I'm not implementing each, because "perform a function for each element of a promise of an array" is too generic an operation to be linked to posts.
// It should come with your promise lib or be implemented elsewhere
exports.addPost = function(post) {
ajax.post(postsURL + "/new", post);
};
var posts = require('./posts');
var post = require('./post');
var target = document.getElementById('posts');
posts.fetch()
.then(function(data) {
data.forEach(function (text) {
var div = document.createElement('div');
post.render(div, text);
target.appendChild(div);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment