1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-26 00:29:47 +02:00

Create section model and push data into the store

This commit is contained in:
zinyando 2016-08-11 15:18:39 +02:00
parent b638d6a114
commit f6febf765c
2 changed files with 63 additions and 40 deletions

17
app/app/models/section.js Normal file
View file

@ -0,0 +1,17 @@
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
// import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
contentType: attr('string'),
title: attr('string'),
description: attr('string'),
iconFont: attr('string'),
iconFile: attr('string'),
hasImage: Ember.computed('iconFont', 'iconFile', function () {
return this.get('iconFile').length > 0;
}),
created: attr(),
revised: attr()
});

View file

@ -14,52 +14,58 @@ import models from '../utils/model';
import BaseService from '../services/base'; import BaseService from '../services/base';
export default BaseService.extend({ export default BaseService.extend({
sessionService: Ember.inject.service('session'), sessionService: Ember.inject.service('session'),
ajax: Ember.inject.service(), ajax: Ember.inject.service(),
store: Ember.inject.service(),
// Returns all available sections. // Returns all available sections.
getAll() { getAll() {
return this.get('ajax').request(`sections`,{ return this.get('ajax').request(`sections`, {
method: 'GET' method: 'GET'
}).then((response)=>{ }).then((response) => {
let data = []; let data = [];
_.each(response, function(obj) { _.each(response, (obj) => {
data.pushObject(models.SectionModel.create(obj)); debugger;
}); let sectionData = this.get('store').normalize('section', obj);
let section = this.get('store').push({ data: sectionData });
data.pushObject(section);
});
return data; return data;
}); });
}, },
// Requests data from the specified section handler, passing the method and document ID // Requests data from the specified section handler, passing the method and document ID
// and POST payload. // and POST payload.
fetch(page, method, data) { fetch(page, method, data) {
let documentId = page.get('documentId'); let documentId = page.get('documentId');
let section = page.get('contentType'); let section = page.get('contentType');
let url = `sections?documentID=${documentId}&section=${section}&method=${method}`; let url = `sections?documentID=${documentId}&section=${section}&method=${method}`;
return this.get('ajax').post(url, { return this.get('ajax').post(url, {
data: JSON.stringify(data), data: JSON.stringify(data),
contentType: "application/json" contentType: "application/json"
}); });
}, },
// Did any dynamic sections change? Fetch and send up for rendering? // Did any dynamic sections change? Fetch and send up for rendering?
refresh(documentId) { refresh(documentId) {
let url = `sections/refresh?documentID=${documentId}`; let url = `sections/refresh?documentID=${documentId}`;
return this.get('ajax').request(url, { return this.get('ajax').request(url, {
method: 'GET' method: 'GET'
}).then((response)=>{ }).then((response) => {
let pages = []; let pages = [];
if (is.not.null(response) && is.array(response) && response.length > 0) { if (is.not.null(response) && is.array(response) && response.length > 0) {
_.each(response, function(page) { _.each(response, (page) => {
pages.pushObject(models.PageModel.create(page)); let data = this.get('store').normalize('page', page);
}); let pageData = this.get('store').push({ data: data });
} pages.pushObject(pageData);
});
}
return pages; return pages;
}); });
} }
}); });