Models

Models

At the most basic level, a model is a custom data container that defines a new type not currently available (as opposed to primitive types like integers, strings and bool which are always available)

@nearBindgen
export class TextMessage {
  sender: string;
  text: string;
  number: u64;
  isRead: bool;
}
// see https://github.com/near/near-sdk-as/blob/master/assembly/__tests__/runtime/model.ts

@nearBindgen is a decorator made for the serialization of custom classes before they are saved to storage onto the blockchain. @nearBindgen does not support class inheritance.

Models can build on top of one another as with the sample below, taken from CryptoCorgis (like Pokemon), which includes 3 models:

  • CorgiMetaData which wraps an array of strings
  • the Corgi model which includes strings, an integer and also uses CorgiMetaData
  • and finally a CorgiArray which includes an array of Corgis and maintains the length of that array as well
@nearBindgen
export class CorgiMetaData {
  dna: Array<string>;
}

export class Corgi {
  owner: string;
  sender: string;
  message: string;
  dna: string;
  name: string;
  color: string;
  backgroundColor: string;
  rate: string;
  sausage: string;
  quote: string;
  level: i32;
  metadata: CorgiMetaData;
}

export class CorgiArray {
  corgis: Array<Corgi>;
  len: i32;
}
// see https://github.com/nearprotocol/corgis/blob/master/assembly/model.ts

Since models are just AssemblyScript classes, they support custom constructors and behavior, not just data, as with the example here:

@nearBindgen
export class Greeter {
  text: string;

  constructor(text: string) {
    this.text = text;
  }

  greet(userId: string): string {
    return "Hello, " + userId;
  }
}
// see https://github.com/nearprotocol/blockbuster/blob/master/assembly/model.ts
Background Pattern
Birlikte öğrenelim

Sektörde en çok aranan yazılım becerilerini kazan

Yapay zeka desteği, birebir mentörlük saatleri, canlı dersler ve senin için özel hazırlanmış içeriklerle eksiklerini tamamla, düzenli geri bildirimler al ve öğrenme sürecini en verimli hale getir.

Yunus Emre Kabakcı

Patika+ mezunu

Patika+ Fullstack Web Development Bootcamp mezunumuz Yunus Emre,

3 ay içinde Katar’dan aldığı teklif ile, global bir şirket olan Pavo Group’da işe başladı!


“İçerik zenginliği, mentor desteği, ileriye dönük bir network sağlaması ve dünyada en çok tercih edilen frameworkler üzerinden bir eğitim veriyor olması Patika+’ı tercih etmemin temel sebepleri oldu!“

Test

Yorumlar

Yorum yapabilmek için derse kayıt olmalısın!

Bu dersi nasıl buldun?

This website uses cookies to ensure you get the best experience.

Disclaimer: The information /programs / events provided on https://patika.dev and https://risein.com are strictly for upskilling and networking purposes related to the technical infrastructure of blockchain platforms. We do not provide financial or investment advice and do not make any representations regarding the value, profitability, or future price of any blockchain or cryptocurrency. Users are encouraged to conduct their own research and consult with licensed financial professionals before engaging in any investment activities. https://patika.dev and https://risein.com disclaim any responsibility for financial decisions made by users based on information provided here.