GithubHelp home page GithubHelp logo

Example about typed-we-app HOT 6 CLOSED

jhcao23 avatar jhcao23 commented on August 26, 2024
Example

from typed-we-app.

Comments (6)

emeryao avatar emeryao commented on August 26, 2024 3
export class ProfilePage implements PageParam, Page {

    setData: (data: any, callback?: Function) => void;

    /**
     * 页面的初始数据
     */
    data: {
        imageUrl: null
    };

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options): void {
        let that: WeApp.Page = this; // <---------- here is the trick 
        if(getApp().globalData.userInfo) {
            that.setData({
                imageUrl: getApp().globalData.userInfo.avatarUrl
            });
        }
    }
... ... ...
... ... ...
... ... ...
}

from typed-we-app.

emeryao avatar emeryao commented on August 26, 2024

detail-page.ts :

let detailPage: WeApp.Page;
Page({
    onload: ()=>{
        // store the page obj
        detailPage = this; 
    },
    onSomeClick: ()=>{
        // now you can get intellisence from WeApp.Page from the page interface when pressing the dot key
        detailPage.setData({foo:'bar'}); 
    }
});

hope that will help you
Thanks for your issue @jhcao23
nice to see you here 😆

from typed-we-app.

jhcao23 avatar jhcao23 commented on August 26, 2024

I see I see, it works! Great!
I'm not an expert on JavaScript. Please allow me to ask another stupid question.
I personally prefer to write class in typescript.
If I write a class like below, how can I take advantage of the ts definition of WeApp.Page and WeApp.PageParam?:

export class ProfilePage implements PageParam, Page {

    setData: (data: any, callback?: Function) => void;

    /**
     * 页面的初始数据
     */
    data: {
        imageUrl: null
    };

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options): void {
        let that = this;
        if(getApp().globalData.userInfo) {
            that.setData({
                imageUrl: getApp().globalData.userInfo.avatarUrl
            });
        }
    }
... ... ...
... ... ...
... ... ...
}

Thanks,
John

from typed-we-app.

jhcao23 avatar jhcao23 commented on August 26, 2024

Hi @emeryao ,

What I really meant is how to use the class definition in some page ts file like your detail-page.ts or my profile.ts...

So after finishing up the ProfilePage.ts file, I will write the profile.ts file for Profile Page.
Then the profile.ts wants to use the ProfilePage class:

let profilePage: ProfilePage = new ProfilePage();
Page(profilePage);

As Page take a PageParam as parameter input and my ProfilePage implements PageParam,
so I guess that passing the profilePage to the Page function as parameter should be fine in theory; however, I'm not quite sure if this is the correct method to use the ProfilePage class.
Please comment or advise.

Thanks,
John

from typed-we-app.

emeryao avatar emeryao commented on August 26, 2024

It spend me a week to understand your requirement up there
but I'm sorry I can't so I couldn't give your any suggestions 😓

from typed-we-app.

jhcao23 avatar jhcao23 commented on August 26, 2024

I know its a little bit tweak but its really simple.
Let's say I want to create a profile page, so I have a profile folder and a few empty files inside it:
profile.ts profile.json profile.wxml profile.wxss, where profile.ts can generate profile.js automatically in WebStorm. Then I create another file ProfilePage.ts which is defined like above thread:

export class ProfilePage implements PageParam, Page {
    setData: (data: any, callback?: Function) => void;
    data: {  imageUrl: null };
    onLoad(options): void {
        let that: WeApp.Page = this;
        if(getApp().globalData.userInfo) {
            that.setData({
                imageUrl: getApp().globalData.userInfo.avatarUrl
            });
        }
    } 
    ... ... ...
}

Now I need to write the profile.ts file. So then I want to use the ProfilePage and map the defined functions to the profile.ts maybe like this:

// profile.js
import {ProfilePage} from './ProfilePage';
const profilePage = new ProfilePage();
// deconstruction does not work so sad
// const {...ddd} = profilePage;
// console.log('ddd', ddd);
// Page({...ddd});

Page({
    data: profilePage.data,
    onLoad: profilePage.onLoad,
    takeNewPhoto: profilePage.takeNewPhoto,
    takePhotosSuccess: profilePage.takePhotosSuccess,
    takePhotoSuccess: profilePage.takePhotoSuccess,
    cameraError: profilePage.cameraError,
    uploadPhoto: profilePage.uploadPhoto,
    uploadPhotoSuccess: profilePage.uploadPhotoSuccess
});

I found this way of writing works but that's a lot of typing and the deconstruction way doesn't work.

The reason I write both profile.ts and ProfilePage.ts files is because I personally don't like the traditional way to write the profile.ts, which doesn't implement Page or PageParam so doesn't have strict and strong types and, its functions are separated by comma , (I really don't like the comma and I prefer to write everything in a normal class first like ProfilePage.ts):

Page(
    {
        data: { imageUrl: null },
        onLoad(options): void {
            let that = this;
            if (getApp().globalData.userInfo) {
                that.setData({
                    imageUrl: getApp().globalData.userInfo.avatarUrl
                });
            }
        },
        takeNewPhoto(): void {
            wx.chooseImage({
                count: 1,
                sizeType: ['original'],
                sourceType: ['album', 'camera'],
                success: this.takePhotosSuccess
            });
        },
        takePhotosSuccess(res?: ChooseImageSuccess): void {
            if (res) {
                console.log('tempImagePage', res.tempFilePaths);
                this.setData({
                    imageUrl: res.tempFilePaths[0]
                });
                this.uploadPhoto(res.tempFilePaths[0]);
            }
        },
        uploadPhoto(tempImagePath: string): void {
            uploadPhoto(tempImagePath, this.uploadPhotoSuccess);
        },
        uploadPhotoSuccess(res?: WeApp.HttpResponse): void {
            console.log('uploadPhotoSuccess', res);
            this.setData({imageUrl: res!!.data});
        }
    }
);

from typed-we-app.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.