Skip to content

Instantly share code, notes, and snippets.

@parrot-studio
Created December 21, 2011 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parrot-studio/1505246 to your computer and use it in GitHub Desktop.
Save parrot-studio/1505246 to your computer and use it in GitHub Desktop.
CoffeeScript+jQuery.tmpl MVC sample (for new app)
(function() {
var Fragment, FragmentController, FragmentView, User,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
$.ajaxSetup({
cache: false
});
User = (function() {
function User(data) {
this.profile_url = __bind(this.profile_url, this); this.provider = data.provider;
this.user_id = data.user_id;
this.user_nickname = data.user_nickname;
this.user_name = data.user_name;
this.user_image = data.user_image;
}
User.prototype.profile_url = function() {
return "https://twitter.com/" + this.user_nickname;
};
return User;
})();
Fragment = (function() {
function Fragment(data) {
this.text = data.text;
this.limit_time = data.limit_time;
this.user = new User(data);
}
return Fragment;
})();
FragmentView = (function() {
var fragment_body_tmpl;
function FragmentView() {
this.fragment_body_view = __bind(this.fragment_body_view, this);
}
fragment_body_tmpl = '<img src=\'${user_image}\' /><br>\nuser_nickname : ${user_nickname}<br/>\nuser_name : ${user_name}<br/>\nprofile_url : ${profile_url}<br/>';
FragmentView.prototype.fragment_body_param = function(frag) {
return {
user_name: frag.user.user_name,
user_nickname: frag.user.user_nickname,
user_image: frag.user.user_image,
profile_url: frag.user.profile_url()
};
};
FragmentView.prototype.fragment_body_view = function(frag) {
return ($.tmpl(fragment_body_tmpl, this.fragment_body_param(frag))).appendTo('#fragment_body');
};
return FragmentView;
})();
FragmentController = (function() {
var code_parser;
function FragmentController() {
this.load_fragment_body = __bind(this.load_fragment_body, this);
this.body_api_url = __bind(this.body_api_url, this); this.view = new FragmentView;
this.code = this.parse_code();
}
FragmentController.prototype.body_api_url = function() {
return "http://" + location.host + "/d/" + this.code;
};
code_parser = /^\/s\/([0-9a-zA-Z]+)$/;
FragmentController.prototype.parse_code = function() {
var _ref;
return (_ref = code_parser.exec(location.pathname)) != null ? _ref[1] : void 0;
};
FragmentController.prototype.load_fragment_body = function() {
var _this = this;
return $.getJSON(this.body_api_url(), {}, function(frag) {
return _this.view.fragment_body_view(new Fragment(frag));
});
};
return FragmentController;
})();
$(function() {
return (new FragmentController).load_fragment_body();
});
}).call(this);
$.ajaxSetup cache: false
class User
constructor: (data) ->
@provider = data.provider
@user_id = data.user_id
@user_nickname = data.user_nickname
@user_name = data.user_name
@user_image = data.user_image
profile_url: => "https://twitter.com/#{@user_nickname}"
class Fragment
constructor: (data) ->
@text = data.text
@limit_time = data.limit_time
@user = new User data
class FragmentView
fragment_body_tmpl = '''
<img src='${user_image}' /><br>
user_nickname : ${user_nickname}<br/>
user_name : ${user_name}<br/>
profile_url : ${profile_url}<br/>
'''
fragment_body_param: (frag) ->
user_name: frag.user.user_name
user_nickname: frag.user.user_nickname
user_image: frag.user.user_image
profile_url: frag.user.profile_url()
fragment_body_view: (frag) =>
($.tmpl fragment_body_tmpl, @fragment_body_param(frag)).appendTo('#fragment_body')
class FragmentController
constructor: () ->
@view = new FragmentView
@code = @parse_code()
body_api_url: => "http://#{location.host}/d/#{@code}"
code_parser = /^\/s\/([0-9a-zA-Z]+)$/
parse_code: -> code_parser.exec(location.pathname)?[1]
load_fragment_body: =>
$.getJSON @body_api_url(), {}, (frag) =>
@view.fragment_body_view(new Fragment frag)
$ -> (new FragmentController).load_fragment_body()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment