0 Votes

Changes for page Todolist Macro

Last modified by Ryan C on 2025/04/30 07:54

From version 26.1
edited by Ryan C
on 2025/04/24 11:19
Change comment: There is no comment for this version
To version 24.2
edited by Ryan C
on 2025/04/24 11:17
Change comment: Synchronized object properties with their current classes

Summary

Details

XWiki.JavaScriptExtension[0]
Code
... ... @@ -21,8 +21,6 @@
21 21  
22 22   // Define the todolist store that will write to the TodoListsService
23 23   Todos.ApplicationAdapter = DS.Adapter.extend({
24 - namespace: 'api', // Moved inside the adapter definition
25 -
26 26   createRecord: function(store, type, record) {
27 27   console.log("createRecord");
28 28   var data = this.serialize(record, { includeId: true });
... ... @@ -29,18 +29,16 @@
29 29   var query = { create: "1", content: JSON.stringify(data) };
30 30  
31 31   return new Ember.RSVP.Promise(function(resolve, reject) {
32 - var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
30 + jQuery.getJSON(
31 + "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
33 33   XWiki.currentSpace + "." + XWiki.currentPage +
34 - "&xpage=plain&outputSyntax=plain";
35 - console.log("Creating record, URL:", url);
36 -
37 - jQuery.getJSON(url, query).then(function(data) {
38 - console.log("Create success:", data);
33 + "&xpage=plain&outputSyntax=plain",
34 + query
35 + ).then(function(data) {
39 39   Ember.run(function() {
40 40   resolve(data);
41 41   });
42 42   }, function(jqXHR) {
43 - console.error("Create error:", jqXHR);
44 44   Ember.run(function() {
45 45   reject(jqXHR);
46 46   });
... ... @@ -67,18 +67,16 @@
67 67   var query = { since: sinceToken };
68 68  
69 69   return new Ember.RSVP.Promise(function(resolve, reject) {
70 - var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
66 + jQuery.getJSON(
67 + "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
71 71   XWiki.currentSpace + "." + XWiki.currentPage +
72 - "&xpage=plain&outputSyntax=plain";
73 - console.log("Finding all records, URL:", url);
74 -
75 - jQuery.getJSON(url, query).then(function(data) {
76 - console.log("FindAll success:", data);
69 + "&xpage=plain&outputSyntax=plain",
70 + query
71 + ).then(function(data) {
77 77   Ember.run(function() {
78 78   resolve(data);
79 79   });
80 80   }, function(jqXHR) {
81 - console.error("FindAll error:", jqXHR);
82 82   Ember.run(function() {
83 83   reject(jqXHR);
84 84   });
... ... @@ -87,7 +87,6 @@
87 87   },
88 88  
89 89   updateRecord: function(store, type, record) {
90 - console.log("updateRecord");
91 91   var recordArrays = store.recordArrayManager.recordArraysForRecord(record);
92 92   if (recordArrays && recordArrays.list && recordArrays.list[0]) {
93 93   return this.saveAll(recordArrays.list[0].content);
... ... @@ -94,32 +94,38 @@
94 94   }
95 95   return Ember.RSVP.resolve();
96 96   },
97 -
98 98   saveAll: function(alldata) {
99 99   console.log("saveAll");
100 100   var query = { save: "1", content: JSON.stringify(alldata) };
101 -
102 - return new Ember.RSVP.Promise(function(resolve, reject) {
103 - var currentPage = XWiki.currentSpace + "." + XWiki.currentPage;
104 - var url = "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
105 - currentPage + "&xpage=plain&outputSyntax=plain";
106 - console.log("Saving all records, URL:", url, "Page:", currentPage);
107 -
108 - jQuery.getJSON(url, query).then(function(data) {
109 - console.log("SaveAll success:", data);
110 - Ember.run(function() {
111 - resolve(data);
112 - });
113 - }, function(jqXHR) {
114 - console.error("SaveAll error:", jqXHR);
115 - Ember.run(function() {
116 - reject(jqXHR);
117 - });
118 - });
119 - });
120 - }
93 +return new Ember.RSVP.Promise(function(resolve, reject) {
94 + jQuery.getJSON(
95 + "${xwiki.getURL('TodoLists.TodoListsService')}?page=" +
96 + (
97 + typeof XWiki !== 'undefined' &&
98 + XWiki.currentSpace &&
99 + XWiki.currentPage
100 + ? XWiki.currentSpace + "." + XWiki.currentPage
101 + : document.location.pathname.split('/').pop()
102 + ) +
103 + "&xpage=plain&outputSyntax=plain",
104 + query
105 + ).then(function(data) {
106 + Ember.run(function() {
107 + resolve(data);
108 + });
109 + }, function(jqXHR) {
110 + Ember.run(function() {
111 + reject(jqXHR);
112 + });
121 121   });
114 +});
122 122  
116 +
117 + // Register the adapter
118 + Todos.ApplicationAdapter.reopen({
119 + namespace: 'api'
120 + });
121 +
123 123   // Routing
124 124   Todos.Router.map(function() {
125 125   this.resource('todos', { path: '' }, function() {
... ... @@ -216,8 +216,6 @@
216 216   var title = this.get('newTitle');
217 217   if (!title || !title.trim()) { return; }
218 218  
219 - console.log("Creating new todo with title:", title);
220 -
221 221   // Create the new Todo model
222 222   var todo = this.store.createRecord('todo', {
223 223   title: title,
... ... @@ -230,15 +230,11 @@
230 230   this.set('newTitle', '');
231 231  
232 232   // Save the new model
233 - todo.save().then(function(savedTodo) {
234 - console.log("Todo saved successfully:", savedTodo);
230 + todo.save().then(function() {
235 235   // Force a refresh of the list after saving
236 236   Ember.run.later(function() {
237 - console.log("Refreshing todo list");
238 238   Todos.store.find('todo');
239 239   }, 100);
240 - }, function(error) {
241 - console.error("Failed to save todo:", error);
242 242   });
243 243   },
244 244