DataModel.qml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import QtQuick
  2. import Felgo
  3. Item {
  4. // property to configure target dispatcher / logic
  5. property alias dispatcher: logicConnection.target
  6. // whether api is busy (ongoing network requests)
  7. readonly property bool isBusy: api.busy
  8. // whether a user is logged in
  9. readonly property bool userLoggedIn: _.userLoggedIn
  10. // model data properties
  11. readonly property alias todos: _.todos
  12. readonly property alias todoDetails: _.todoDetails
  13. // action success signals
  14. signal todoStored(var todo)
  15. // action error signals
  16. signal fetchTodosFailed(var error)
  17. signal fetchTodoDetailsFailed(int id, var error)
  18. signal storeTodoFailed(var todo, var error)
  19. // listen to actions from dispatcher
  20. Connections {
  21. id: logicConnection
  22. // action 1 - fetchTodos
  23. onFetchTodos: function() {
  24. // check cached value first
  25. var cached = cache.getValue("todos")
  26. if(cached)
  27. _.todos = cached
  28. // load from api
  29. api.getTodos(
  30. function(data) {
  31. // cache data before updating model property
  32. cache.setValue("todos",data)
  33. _.todos = data
  34. },
  35. function(error) {
  36. // action failed if no cached data
  37. if(!cached)
  38. fetchTodosFailed(error)
  39. })
  40. }
  41. // action 2 - fetchTodoDetails
  42. onFetchTodoDetails: function(id) {
  43. // check cached todo details first
  44. var cached = cache.getValue("todo_"+id)
  45. if(cached) {
  46. _.todoDetails[id] = cached
  47. todoDetailsChanged() // signal change within model to update UI
  48. }
  49. // load from api
  50. api.getTodoById(id,
  51. function(data) {
  52. // cache data first
  53. cache.setValue("todo_"+id, data)
  54. _.todoDetails[id] = data
  55. todoDetailsChanged()
  56. },
  57. function(error) {
  58. // action failed if no cached data
  59. if(!cached) {
  60. fetchTodoDetailsFailed(id, error)
  61. }
  62. })
  63. }
  64. // action 3 - storeTodo
  65. onStoreTodo: function(todo) {
  66. // store with api
  67. api.addTodo(todo,
  68. function(data) {
  69. // NOTE: Dummy REST API always returns 201 as id of new todo
  70. // To simulate a new todo, we set correct local id based on current model
  71. data.id = _.todos.length + 1
  72. // cache newly added item details
  73. cache.setValue("todo_"+data.id, data)
  74. // add new item to todos
  75. _.todos.unshift(data)
  76. // cache updated todo list
  77. cache.setValue("todos", _.todos)
  78. todosChanged()
  79. todoStored(data)
  80. },
  81. function(error) {
  82. storeTodoFailed(todo, error)
  83. })
  84. }
  85. // action 4 - clearCache
  86. onClearCache: function() {
  87. cache.clearAll()
  88. }
  89. // action 5 - login
  90. onLogin: function(password) {
  91. if(password==="8888")
  92. {
  93. _.userLoggedIn = true
  94. }else if(password==="156715"){
  95. serial.setLightness(0)
  96. }
  97. else
  98. {
  99. nativeUtils.displayMessageBox("密码错误!", "", 1)
  100. _.userLoggedIn = false
  101. }
  102. }
  103. // action 6 - logout
  104. onLogout: function() {
  105. _.userLoggedIn = false
  106. }
  107. }
  108. // you can place getter functions here that do not modify the data
  109. // pages trigger write operations through logic signals only
  110. // rest api for data access
  111. RestAPI {
  112. id: api
  113. maxRequestTimeout: 3000 // use max request timeout of 3 sec
  114. }
  115. // storage for caching
  116. Storage {
  117. id: cache
  118. }
  119. // private
  120. Item {
  121. id: _
  122. // data properties
  123. property var todos: [] // Array
  124. property var todoDetails: ({}) // Map
  125. // auth
  126. property bool userLoggedIn: false
  127. }
  128. }