Angular:对象结构的最佳实践(Angular: object structure best practice)
我在Angular工作,目前专注于过滤嵌套对象。 这是我的对象的结构:
$scope.subjectBin = { "Faculty of Engineering": { "ECE": [{<course-object>},{<course-object>}], "CHEM: [{<course-object>}] }, "Faculty of Science": { "BIOL: [{<course-object>},...], ... }, ... }
当我使用过滤这个对象,构建新功能,并在分层手风琴中呈现它时,我想我应该改变这个对象的设计。
有人建议将对象更改为以下内容:
$scope.subjectBin = [{ faculty: "Faculty of Engineering", subjects: [{ subjectName: "ECE", courses: [{<course-object>},{<course-object>}] }, ... ] }, { faculty: "Faculty of Science", subjects: [{ subjectBin: "CMPUT", courses: [{<course-object>},...] }, ... ] }]
我被告知这种布局更好,因为:“它不使用对象作为键盘图,它遵循一种模式”。
我同意它更好,但总体上希望对对象布局做一些澄清。
布置嵌套对象的 最佳做法是什么? 具有固定键名的基于数组的对象是否更可取?
I'm working in Angular, currently focusing on filtering a nested object. Here is the structure of my object:
$scope.subjectBin = { "Faculty of Engineering": { "ECE": [{<course-object>},{<course-object>}], "CHEM: [{<course-object>}] }, "Faculty of Science": { "BIOL: [{<course-object>},...], ... }, ... }
As I work with filtering this object, build new features, and render this in a layered accordion, I'm thinking I should change the design of this object.
Someone suggested to change the object to the following:
$scope.subjectBin = [{ faculty: "Faculty of Engineering", subjects: [{ subjectName: "ECE", courses: [{<course-object>},{<course-object>}] }, ... ] }, { faculty: "Faculty of Science", subjects: [{ subjectBin: "CMPUT", courses: [{<course-object>},...] }, ... ] }]
I was told this layout is better because: "it doesn't use objects as keymaps and it follows a pattern".
I agree that it's better but would like some clarification on object layout, in general.
What are best practices for laying out nested objects? Are array-based objects with fixed key-names preferable?
原文:https://stackoverflow.com/questions/26953495
最满意答案
这里有几件事需要考虑,可以帮助您摆脱错误并获得更清晰的代码:
- 应使用AlarmManager计划警报
- ServiceGps可以由WakefulBroadcastReceiver启动,因此您不必自己处理唤醒锁定。
- 如果目标低于19,您可以从AlarmManager中使用setRepeating ...方法中受益。
**不要忘记你的Manifest中的权限。
希望能帮助到你。
There are several things to consider here that can help you get rid of your errors and get a cleaner code:
- The alarm should be scheduled using the AlarmManager
- The ServiceGps could be launched by a WakefulBroadcastReceiver, so you don't have to handle wake lock yourself.
- If the target is below 19, you could benefit from using setRepeating... method in the AlarmManager.
** Don't forget the permissions in your Manifest.
Hope it helps.
相关问答
更多-
你不能安全地从IntentService注册监听IntentService ,因为一旦onHandleIntent() (aka, doWakefulWork() )完成, doWakefulWork()就会消失。 相反,您需要使用常规服务,以及处理超时等详细信息(例如,用户位于地下室且无法获取GPS信号)。 You cannot safely register listeners from an IntentService, as the IntentService goes away as soon a ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
处理程序无法将消息或Runnable传递到主线程(Handler fails to deliver a message or a Runnable to the main thread)[2023-04-16]
Handler需要一个Looper才能工作。 Looper提供了Handler所需的消息队列。 Activity所有实例都有一个Looper用于处理UI事件,但您可以在其他位置创建Looper实例。 看看你的日志输出,看看Android是否抱怨没有Looper 。 如果是这样,您可以通过将以下内容添加到onCreate()方法的顶部来进行修复: Looper.prepare(); m_handler = new Handler(); Looper.run(); 然后从你的代码中移除m_handler的初 ... -
这里的问题是您正在IntentService管理的线程中创建一个Toast 。 系统将使用与此线程相关联的Handler来显示和隐藏Toast 。 首先, Toast将被正确显示,但当系统尝试隐藏它时,在onHandleIntent方法完成之后,将会抛出错误“向onHandleIntent线程发送消息给Handler”,因为创建Toast的线程是不再有效,而且Toast不会消失。 为了避免这种情况,您应该显示Toast向主线程发送消息。 这里有一个例子: // create a handler to ...
-
这里有几件事需要考虑,可以帮助您摆脱错误并获得更清晰的代码: 应使用AlarmManager计划警报 ServiceGps可以由WakefulBroadcastReceiver启动,因此您不必自己处理唤醒锁定。 如果目标低于19,您可以从AlarmManager中使用setRepeating ...方法中受益。 **不要忘记你的Manifest中的权限。 希望能帮助到你。 There are several things to consider here that can help you get rid ...
-
如果你在android / os / MessageQueue.java中检查源代码,你可以看到类似下面的内容 if (mQuiting) { RuntimeException e = new RuntimeException( msg.target + " sending message to a Handler on a dead thread"); Log.w("MessageQueue", e ...
-
在等待来自单独线程的响应时保持处理程序和服务活动?(Keep handler and service alive while waiting for response from separate thread?)[2022-08-14]
IntentService已经为onHandleIntent()使用了后台线程。 因此,不要使用AsyncTask - 只需在onHandleIntent()中执行代码。 查看https://groups.google.com/forum/#!topic/android-developers/YDrGmFDFUeU IntentService already uses a background thread for onHandleIntent(). Hence, do not use AsyncTask ... -
好吧,我不知道为什么,但dispatchMessage(),而不是sendMessage()解决了问题... well, I dunno why, but dispatchMessage() instead of sendMessage() solved the problem...
-
线程事件的中断处理程序(Interrupt Handler for Thread Event)[2022-08-15]
您仍然没有提到是否使用消息/事件循环框架,这将为您提供一种方法来调用“主”线程并调用事件处理程序。 假设你没有,那么你不能只是打断或调用主线程。 但是,您不需要在确定是否需要创建新线程时才需要知道线程是否处于活动状态。 你可以在这个时候进行检查。 这样,您只需要一种方法来在线程之间传递“完成”状态。 有很多方法可以做到这一点(我从未使用.isAlive(),但您可以在队列,事件甚至共享变量中传回信息)。 使用Event它看起来像这样: class supervisor(): def __init__ ... -
处理与任何其他终止线程没有区别。 之前发生的一件事是根据规则(特定的Thread , ThreadGroup ,所有线程)搜索UncaughtExceptionHandler ,但除此之外,接下来是“正常”清理过程。 与“正常”终止相比,当线程被未捕获的异常终止时,没有关于系统资源(取决于Thread实现)或内存问题的具体后果。 The handling is not different than with any other terminated thread. The one thing that ha ...