{{#user}} <div>this.name</div> {{/user}}
then if var user = {name:''}
will turn out <div></div>
var user = { age:15, name:'' }
{{#user}} <div class="{{this.age < 18+1*Math.random() ? 'illegal' : 'legal'}}">this.name </div> {{/user}}
<div class='illegal'></div>
<div on-eventName='callbackName'>
ractive.on('callbackName',function(e){});
{{#user}} <-- this=user --> <div on-click='alert_username'>{{this.username}}</div> {{/user}}
function(e){ var user = e.context; alert(user.username); }
ractive.data = {'replys':[ { md_text:" ", date:10240912834091,// nix // user:{ username:' ', image:'http://link-roga-serega-avatar.ru' }, // replys:[{ md_text:" ", date:10240912834091, replys:[...]}, {...}] }, { md_text:" ", date:10240912834091,// nix user:{ username:' ' }, ]};
{{#top_reply}} <!-- . , --> {{#if replys.length > 0}} <!-- - --> {{#if this.reply_draft}} <!-- comment_form - .--> {{>comment_form}} {{else}} <!-- , ' ' --> <!-- reply e.context = this; this = top_reply --> <button class="add_comment" on-click="reply"> </button> {{/if}} {{/if}} {{/top_reply}} <!-- partial --> {{#replys}} {{>comment}} {{/replys}} <!-- --> {{#bottom_reply}} {{>comment_form}} {{/bottom_reply}} <!-- {{>comment}} --> <!-- --> <!-- partial replys - this --> <article id="{{this.id}}" class="comment" intro="scroll_to:{go:{{this.is_new}}}"> <header> <!-- --> <img class="avatar" src=""/> <span class="author">{{this.user.username}}</span> <!-- . moment . data--> <time pubdate datetime={{moment(parseInt(date)).format()}}> {{moment(parseInt(date)).fromNow()}} </time> <!-- - --> {{#if user.id === current_user.id}} <button class="delete" on-click="delete" disabled="{{deleting}}"></button> {{/if}} </header> <!-- --> {{{marked(md_text)}}} <footer> <!-- --> {{^this.reply_draft}} <button on-click="reply"></button> {{/this.reply_draft}} </footer> <!-- --> {{#if this.reply_draft}} {{>comment_form}} {{/if}} <!-- --> {{#this.replys}} {{>comment}} {{/this.replys}} </article> <!-- {{/comment}} --> <!-- {{>comment_form}} --> <article class="comment"> <header> <img class="avatar" src=""/> <span class="author">{{current_user.username}}</span> </header> <!-- save--> <!-- this .context .--> <form on-submit="save" on-reset="cancel_reply"> <!-- input . --> <!-- textarea this.reply_draft.text--> <textarea value="{{this.reply_draft.text}}" placeholder=" "></textarea> <!-- --> <button type="submit" disabled="{{this.loading}}"></button> <button type="reset"></button> </form> </article> <!-- {{/comment_form}} -->
function Comment(id, md_text, user, domain, date, reply_to) { if (!md_text || !user || !domain || !date) { throw new Error('md_text, user, domain and date is required'); } this.id = id; this.md_text = md_text; this.user = user; this.date = date; this.reply_to = reply_to ? reply_to.id || reply_to : null; this.domain = domain; this.replys = []; }; Comment.prototype.destroy = function () { //ajax }; Comment.prototype.save = function () { //ajax }; Comment.fetch = function (domain, options) { // . };
var Comments = Ractive.extend({ //$(selector) Ractive el: '#comments', //id tag <script> ( ) template: '#comments_template', init : function (options) { var self = this; // . var domain = options.comments_domain; // . var current_user = options.current_user; if (!current_user) { throw new Error('options.domain and options.current_user is required'); } // . Comment .fetch(domain) .then(function (comments) { // . var comments_by_id = _.indexBy(comments, 'id'); // comments = _.sortBy(comments, 'date'); // // replys , . comments = _.filter(comments, function (comment) { if (comment.reply_to && comments_by_id[comment.reply_to]) { var parent = comments_by_id[comment.reply_to]; parent.replys.push(comment); } return comment.reply_to === null }); //reative.set(prop_name,value) data, ractive.get(prop_name) data. // . // self.data.replys = comments . self.set('replys', comments); }); //Ractive . //Events ractive.on('event_name',callback); .on({prop_name:callback}); //Events ... . self.on({ // . // . ractive //e.node - html DOM . //e.original - DOM ( e.original.preventDefault()); //e.keypath - . ractive.data . data : //data:{ comments:['text1','text2'] }; 'text2' 'comments.1' //Ractive DOM , DOM keypath . // e.context - e.keypath ( ractive.get(e.keypath) ); save : function (e) { e.original.preventDefault(); //save . , , - - . var reply_to = e.context.id ? e.context.id : null; // . var reply = e.context.reply_draft; // var new_comment = new Comment(void 0, reply.text, current_user, domain, moment().valueOf(), reply_to); // self.set(e.keypath + '.loading', true); // new_comment.save() .then(function (comment) { // setTimeout(function(){ // , . self.set(e.keypath + '.loading', false); self.set(e.keypath + '.reply_draft', false); var comments = reply_to ? self.get(e.keypath + '.replys') : self.get('replys'); // comments.push(comment); //. , . // Ractive . // - , . },600); }) .fail(function (err) { self.set(e.keypath + '.loading', false); }); }, delete : function (e) { // . // . // . self.set(e.keypath + '.deleting', true); e .context .destroy() .then(function (comment) { self.set(e.keypath + '.md_text', comment.md_text); self.set(e.keypath + '.deleting', false); }) .fail(function () { self.set(e.keypath + '.deleting', false); //TODO: show erorr; }); }, reply : function (e) { //e.keypath . // . // . Comments... v2.0 self.set(e.keypath + '.reply_draft', {}); }, cancel_reply: function (e) { // ... // self.set(e.keypath + '.reply_draft', false); } }); }, //data . data: { bottom_reply: {}, // top&bottom_reply , . top_reply: {}, marked: marked, //data , . moment: moment // . } });
//Ractive \ DOM . transitions: { scroll_to: function (t, param) { // (param.go === true) if (t.isIntro && param.go) { // . // $ var element = $(t.node); var offsetTop = element.offset().top - element.outerHeight(); $('html, body').animate({scrollTop: offsetTop}, 500, function () { // t.setStyle('background-color', 'yellow'); t.animateStyle('background-color', 'transparent', { duration: 1500, easing : 'easing' }, t.complete); // Ractive t.complete . Ractive . }); } else { // DOM, (param.go) t.complete(); } } }
Source: https://habr.com/ru/post/235675/
All Articles