+/* article-item2.js */
+
var articleItem = {};
-articleItem.run = function run(params) {
+articleItem.init = function init () {
function data() {
var data = {
// for editor
}
var props = {
- item: Object,
- editing: {type: Boolean, default: false},
+ item: {
+ type: Object,
+ default: function () {return new Contents();}
+ },
+ editing: {type: Number, default: 0},
};
var watch = {
showPreview: showPreview,
postSubmission: postSubmission,
leavePreview: leavePreview,
-// quoteHtml: quoteHtml,
-// quoteTitle: quoteTitle,
startEdit: startEdit,
cancelEdit: cancelEdit,
};
};
function created() {
+ if (page.type == 'story') {
+ // load story data
+ var url = "/api/v1/story?stoid=" + page.stoid;
+ loadItem(this, url);
+ }
if (this.item.submission_id) {
// load submission data
var url = "/api/v1/submission?subid=" + this.item.submission_id;
}
}
- function initItem(data) {
- data.item.author = user.nickname;
- var date = new Date();
- data.item.createtime = nsUtil.toDateTimeLocal(date);
+ function loadItem(vue, url) {
+ vue.$http.get(url).then(
+ (resp) => { // succeed
+ for (k in resp.body.item) {
+ vue.item[k] = resp.body.item[k];
+ }
+ //vue.title = resp.body.item.title;
+ //vue.introtext = resp.body.item.introtext;
+ initItem(vue);
+ },
+ (resp) => { //failed
+ vue.message = resp.body.message;
+ initItem();
+ });
+ }
+
+ function initItem(vue) {
+ // copy item property to vm
+ for (k in vue.$data) {
+ if (vue.item[k]) {
+ vue[k] = vue.item[k];
+ }
+ }
+
+ vue.item.author = vue.item.author || user.nickname;
+ if (!vue.item.createtime) {
+ var date = new Date();
+ vue.item.createtime = nsUtil.toDateTimeLocal(date);
+ }
}
Vue.component('article-item', {
- template: '#article-item',
+ template: '#article-item-template',
props: props,
data: data,
computed: computed,
});
};
-articleItem.run();
+articleItem.init();
+
+articleItem.run = function (params) {
+ params = params || {};
+ if (!params.el) {
+ console.log('error in articleItem.run(): no element given');
+ return;
+ }
+ this.vm = new Vue({el: params.el});
+};
+
+articleItem.addTrigger = function addTrigger (selector, params) {
+ params = params || {};
+ if (!params.el) {
+ console.log('error in articleItem.addTrigger(): no element given');
+ return;
+ }
+ if (!selector) {
+ console.log('error in articleItem.addTrigger(): no selector given');
+ return;
+ }
+ $(selector).on('click', function (e) {
+ articleItem.run(params);
+ });
+};
+