微信小程序+知曉云上手TodoList

開發(fā)文檔

https://doc.minapp.com/

后臺創(chuàng)建

入口

授權(quán)

插件版SDK

插件的思路確實很獨特,幫用戶省去一些配置,比如設(shè)定安全服務(wù)域名

初始化校驗

校驗通過

如果看到如上界面,代碼校驗通過

用戶表

通過查看用戶表,確實通過wx.BaaS.auth.loginWithWechat()創(chuàng)建了一個用戶

CURD

原型

參考文檔

https://doc.minapp.com/js-sdk/schema/#wechat-32

報錯信息

未建表報錯如下:Error: 404: not found
未建字段報錯如下:Error: 400: The payload does not match the

數(shù)據(jù)添加結(jié)果

既要建表還要建字段,這跟之前用過的BaaS【LeanCloud Bmob 微信云】都不相同,哪怕是微信云開發(fā),也只是要建好表,保證表存在就可以。

增加數(shù)據(jù)
// 獲取文本框里的內(nèi)容
const title = e.detail.value
// 如果文本為空,給出toast提示
if (!title) {
  wx.showToast({
    title: '請輸入內(nèi)容'
  })
  return
}
let tableName = 'Todo'
// 通過 `tableName` 實例化一個 `TableObject` 對象,操作該對象即相當(dāng)于操作對應(yīng)的數(shù)據(jù)表
let Todo = new wx.BaaS.TableObject(tableName)
// 本地創(chuàng)建一條空記錄
let todo = Todo.create() // todo 為 TableRecord 實例

let item = {
  title: title
}

// 為上面創(chuàng)建的空記錄賦值,并保存到服務(wù)器,save() 方法返回一個 Promise 對象
todo
  .set(item)
  .save()
  .then(res => {
    console.log(res)
    wx.showToast({
      title: '保存成功'
    })
  })

添加成功后,數(shù)據(jù)同時返回一份到res.data中,方便二次使用,而不用再去查詢一遍數(shù)據(jù)庫。

回顯數(shù)據(jù)
讀取數(shù)據(jù)

返回的數(shù)據(jù)在res.data.objects中

數(shù)據(jù)讀取

代碼如下

loadData() {
 let Todo = new wx.BaaS.TableObject('Todo')
 const query = new wx.BaaS.Query()

 Todo.setQuery(query)
   .find()
   .then(res => {
     // find 方法返回值為一個 Promise
     console.log(res)
     const todos = res.data.objects
     this.setData({
       todos: todos
     })
   })
},
修改數(shù)據(jù)
edit(e) {
  // 獲取input組件上的取值
  const title = e.detail.value
  // 設(shè)定currentIndex值
  const index = e.currentTarget.dataset.index
  // 獲取原來數(shù)據(jù)源
  let todos = this.data.todos
  // 修改當(dāng)前元素的title值
  const recordID = todos[index]._id
  // 更新 tableName 為 'Todo' 的數(shù)據(jù)表中 id 為 currentId 的數(shù)據(jù)行的 title 字段
  let tableName = 'Todo'

  let Todo = new wx.BaaS.TableObject(tableName)
  let todo = Todo.getWithoutData(recordID)

  todo.set('title', title)
  todo.update().then(
    res => {
      // success
      wx.showToast({
        title: '修改成功'
      })
      this.loadData()
      this.setData({
        currentIndex: -1
      })
    },
    err => {
      console.log(err)
      // err
    }
  )
},

順便提一下,如果在js代碼中要讀取數(shù)據(jù)對象的屬性,直接使用點語法就可以,不必使用.get(),其實也沒有.get()方法,這跟之前用的BaaS有所不同

刪除
// 刪除 tableName 為 'Todo' 的數(shù)據(jù)表中 recordID 的數(shù)據(jù)項
let tableName = 'Todo'
let recordID = this.data.todos[item]._id

let Todo = new wx.BaaS.TableObject(tableName)
Todo.delete(recordID).then(
  res => {
    // success
    this.loadData()
    wx.showToast({
      title: '刪除成功'
    })
    // checkIndices將它復(fù)位
    this.setData({
      checkIndices: []
    })
  },
  err => {
    // err
  }
)

【正文完】

源碼

https://gitee.com/laeser/demo-weapp 代碼位于pages/todo-zx文件夾下

關(guān)注我

mp
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容