如何在Svelte中有条件地禁用字段?(How to disable field conditionally in Svelte?)
在Angular 2+中(例如),我可以使用这种语法来有条件地禁用一个字段:
<input [disable]="booleanCondition" type="text">
在Svelte中,我试图按照如下方式执行操作,但它不起作用:
<input {booleanCondition ? 'disabled=""' : ''} type="text">
我该怎么做?
In Angular 2+ (for example), I can use this syntax to disable a field conditionally:
<input [disable]="booleanCondition" type="text">
In Svelte I'm trying to do as follows, but it doesn't work:
<input {booleanCondition ? 'disabled=""' : ''} type="text">
How can I do it?
原文:https://stackoverflow.com/questions/50352748
最满意答案
通常,在软件工程中,存储库用于从应用程序的其余部分(通常直接是业务层)抽象数据层(数据库,Web服务),一个很好的例子就是预订网站的这种模式:
它通过发布/订阅异步连接接收更新,并将它们发送到其他组件。 因此组件彼此独立。
因此,Repository只是一个简单的中介类,用于使应用程序更加模块化,以便您可以更轻松地交换部分,并确保应用程序的其余部分不会打扰数据库连接或HTTP调用等等。 从技术上讲,您可以从Application(或其他任何东西)继承并使用它来保存静态变量等等。但正如这里所解释的:
应用程序对象不能保证永远留在内存中,它会被杀死。 与流行的看法相反,该应用程序将不会从头开始重新启动。 Android将创建一个新的Application对象并启动用户之前的活动,以便首先假定应用程序从未被杀死。
所以这完全取决于你,你可以根据自己的意愿使用存储库样式,它根本不与Room或其他任何东西联系在一起。
In general, in Software Engineering a Repository is used to abstract the Data Layer (Database, Web Service) from the rest of the application (usually directly Business Tier), a good example would be this schema of a booking website:
It receives the updates via the Publish/Subscribe asynchronous connection and sends them to the other components. So the components are independent of each other.
So Repository is just a simple mediator class that is used to make the application more modular, so that you can swap out pieces easier, and make sure that the rest of the app doesn't bother with DB connections or HTTP calls and so on. So technically, you can inherit from Application (or anything else) and use it to save static variables and so on.But as explained here:
The application object is not guaranteed to stay in memory forever, it will get killed. Contrary to popular belief, the app won’t be restarted from scratch. Android will create a new Application object and start the activity where the user was before to give the illusion that the application was never killed in the first place.
So it's entirely up to you, you can use the repository style as you wish, it's not at all tied to Room or anything else.
相关问答
更多-
如何将WearableActivity与LiveData和ViewModel一起使用(How can I use WearableActivity with LiveData and ViewModel)[2023-02-13]
更新: AmbientMode似乎是处理此问题的新方法。 正如@codingjeremy在他的帖子中提到的,他们现在已经在Android支持库27.0.0中引入了它。 原版的: 我通过将WearableActivity中的实现逐字复制到我自己的项目中定义的新WearableFragmentActivity类,然后将其更改为扩展FragmentActivity来解决这个问题。 直接获得Google的支持将是理想的解决方案,但如果您在此期间寻找解决方法,这应该可行。 Update: AmbientMode se ... -
在您的情况下,将LiveData
替换为LiveData - >就足以解决您的问题。 但是,我建议尝试在官方示例中提供的RealmLiveResults类: /**
* This class represents a RealmResults wrapped inside a LiveData.
*
* Realm will always keep the RealmResults up-to-date whenever a change occurs on a ...
-
使用LiveData转换获取Room关系数据库值(Fetching Room relational database values with LiveData Transformations)[2023-11-23]
我并不完全处于相同的情况,但下面是我如何处理使用switchMap和RxJava关系获取数据。 也许它可以帮到你。 public LiveData- > fetchAllItems() {
LiveData
- > itemLiveData = mItemDao.fetchAllItems();
itemLiveData = Transformations.switchMap(itemLiveData, input -> {
MutableL ...
-
所以,我最终这样做了: 该片段过滤ViewModel的状态。 副作用:过滤器状态可以被多个(即,随后由于配置改变)片段实例使用。 也许你想要,也许不是。 我做。 ViewModel包含一个MediatorLiveData实例。 它有一个来源:Room DB LiveData对象。 源代码简单地改变为中介。 如果过滤器被片段更改,则源将被重新调换。 回答我的详细问题: 没有后过滤 是的,需要更换过滤器 我不重用ComputableLiveData(不确定这是否可能) 关于评论中的讨论: 我不应用分页 关于房间 ...
-
你的函数getUnloadedOffers()是否返回空的mutableList? 我不确定是否将items传递给itemsLiveData会起作用,因为观察LiveData正在bg线程上运行(如果我没有弄错) Does your function getUnloadedOffers() return empty mutableList? I'm not sure if passing items to itemsLiveData is going to work, since observing Live ...
-
通常,在软件工程中,存储库用于从应用程序的其余部分(通常直接是业务层)抽象数据层(数据库,Web服务),一个很好的例子就是预订网站的这种模式: 它通过发布/订阅异步连接接收更新,并将它们发送到其他组件。 因此组件彼此独立。 因此,Repository只是一个简单的中介类,用于使应用程序更加模块化,以便您可以更轻松地交换部分,并确保应用程序的其余部分不会打扰数据库连接或HTTP调用等等。 从技术上讲,您可以从Application(或其他任何东西)继承并使用它来保存静态变量等等。 但正如这里所解释的: 应用程 ...
-
房间 - LiveData未触发(Room - LiveData not triggered)[2023-05-02]
问题出在AppCompatActivity ,它将ViewModel与LiveData一起LiveData 。 LiveData观察器仅针对Fragment调用,而不是针对Activity调用。 我使用了实现LifecycleOwner接口的AppCompatActivity ,但正确的是实现LifecycleRegistryOwner 。 致谢: https : //issuetracker.google.com/issues/63764057 The problem was inside AppComp ... -
SyncAdapter中的Singleton Room数据库用于触发LiveData(Singleton Room database in SyncAdapter to trigger LiveData)[2024-01-27]
当您希望在不同Threads之间轻松进行通信时,您需要处于同一个进程中。 在AndroidManifest.xml您可以在活动 , 服务 (与SyncAdapters相关), 内容提供商和广播接收器上指定一个名为android:process=":processName"的属性,它可以帮助您超越堆的默认限制(内存)对于单一过程。 这些是总结的PRO / CONS: 多进程PRO:你有更多的内存来运行你的应用程序,如果进程崩溃,它不会崩溃其他的。 多进程CONS:让进程相互通信要困难得多(但并非不可能)但显然 ... -
LiveData如何知道何时在房间数据库中更改数据?(How does LiveData know when data is changed in Room Database?)[2022-03-05]
在Android Room代码中潜水后,我发现了一些东西: Room annotation处理器使用javapoet库从Room annotations( @Query , @Query ...)生成代码 根据查询的结果类型( QueryMethodProcessor ),它使用“binder”或另一个。 在LiveData的情况下,它使用LiveDataQueryResultBinder 。 LiveDataQueryResultBinder生成一个LiveData类,其中包含一个Invalidation ... -
请考虑Google的架构组件教程中的以下代码: 这不是教程。 它是一些代码片段的参考,其中许多代码片段无法编译,因为它们用于说明语法和调用结构,而不是实际运行。 请注意LiveData对象在声明期间被实例化。 这就是编写代码片段的人选择做的事情。 所以谷歌的建议是在声明期间实例化它们。 谷歌没有提出这样的建议。 您引用的代码片段就是一个例子 ,仅此而已。 为什么在声明期间应该实例化LiveData对象? 他们不具有初始值设定项的字段。 如果您愿意,欢迎您在构造函数中实例化它们。 ViewModel具有关于构 ...