Created
June 7, 2014 02:25
-
-
Save j201/be461eda905889516575 to your computer and use it in GitHub Desktop.
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
var marked = require('marked'); | |
var highlight = require('my-code-highlighter'); | |
exports.render = function(el, text) { | |
el.innerHTML = marked(text, { highlight: highlight }); | |
}; |
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
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); | |
}; |
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
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